unity笔记

单例:

public class GameManager : MonoBehaviour
{

    public static GameManager _instance;


  private void Awake()
    {
        _instance = this;
    }
}

 碰撞(要勾选BoxCollider属性的isTrigger):

void  OnTriggerEnter2D(Collider2D other)            //碰撞的触发
    {
        if(other.tag == "Enemy")                        //和敌人的碰撞
        {
            if(!other.GetComponent<Enemy>().isDeath)
            {
                other.gameObject.SendMessage("BeHit");  //调用碰撞方法
       
            }
        }
    }

 

每帧调用图片做动画:

           //主角的运动效果(update里面调用)
            timer += Time.deltaTime; //int frameIndex = (int)(timer / (1f / frameCountsPersecond));
            int frameIndex = (int) (timer*frameCountsPersecond);
            int frame = frameIndex%2;
            spriteRender.sprite = sprites[frame];        

保存最高记录:

        float historyScore = PlayerPrefs.GetFloat("historyHighScore", 0);
        if (nowScore > historyScore)
        {
            PlayerPrefs.SetFloat("historyHighScore", nowScore);
        }
        highScore.text = historyScore + "";
        this.nowScore.text = nowScore + "";

 

打包出来时候的路径:

#if UNITY_EDITOR || UNITY_STANDALONE_WIN || UNITY_STANDALONE
        string path = Application.dataPath + "/StreamingAssets/" + path1 + ".xml"; //文件的路径
#elif UNITY_ANDROID
        string path = "jar:file://" + Application.dataPath +"!/assets/"+ path1 + ".xml";
#elif UNITY_IPHONE
        string filepath = Application.dataPath +"/Raw/"+ path1 + ".xml";
#endif

 

你可能感兴趣的:(unity笔记)