using UnityEngine; using System.Collections; public class Manager : MonoBehaviour { private static Manager m_Instance; public static Manager instance { get { return m_Instance; } } // Use this for initialization void Start () { m_Instance = this; } public void Gprint() { Debug.Log("Gprint"); } public static void print() { Debug.Log("this is Manager"); } // Update is called once per frame void Update () { } }
下面脚本调用
using UnityEngine; using System.Collections; public class test : MonoBehaviour { private Manager maager; void Start () { } // Update is called once per frame void Update () { maager = Manager.instance; maager.Gprint(); Manager.print(); } void OnGUI() { // GUI.DrawTexture(new Rect(0, 0, Screen.width, Screen.height), tex); } }
maager = Manager.instance; 放到update里面,是因为每个脚本的Start执行时间不一样,不能确保manager的start就一定在Test类的前面执行,所以放到update里面更保险类的成员函数就使用得到的,静态的方法使用类的头
时间: 2024-10-12 07:49:54