2013년 11월 28일 목요일

Texture Packer in Unity_Step4. Texture Packer Helper제어를 통한 Sprite Texture 구현


1. Step3에서 만들어놓은 Test Plane에다가 아래의 Script를 추가


Sprite.cs
using UnityEngine;
using System.Collections;

public enum SpriteType {
    Once,
    Loop,
}

public class Sprite : MonoBehaviour {
    
    public SpriteType _Type;
    public string _TextureName; // texture Name
    public int _MaxFrame; // Texture Frame Length
    public float _Dealay; // Sprite play Dealay 
    
    private TPHelper _TPH;
    private int _NowFrame; // now frame Num
    void Awake() {
        _TPH = GetComponent<TPHelper>();
    }
    
    // Use this for initialization
    void Start () {
        _NowFrame = 1;
        StartCoroutine(PlaySprite());
    }
    
    // Update is called once per frame
    void Update () {
    
    }
    
    IEnumerator PlaySprite() { //is Recursion Function
        _NowFrame++;
        yield return new WaitForSeconds(_Dealay);
        _TPH.OnTextureChange(_TextureName + _NowFrame.ToString()); 
        
        if(_NowFrame == _MaxFrame && _Type == SpriteType.Loop) _NowFrame = 0;
        if(_NowFrame == _MaxFrame && _Type == SpriteType.Once) yield return null;
        if(_NowFrame != _MaxFrame) { 
            StartCoroutine(PlaySprite()); 
        }
    }
}




2. Inspector창에서 Property값 변경 





3. Play 후 확인.

ps. 가장 최대한 이해하기 쉽게끔 작성한 코드라서 난이도 측면에서는 아주 쉬우실듯.
이런식으로 TPHelper 컴포넌트에 접근하여서 Texture 수정 , Atlas변경을 할수있으니 
입맛대로 코딩하셔서 사용하시면됩니다 ^ㅡ^

0 개의 댓글:

댓글 쓰기