public class Cursor : MonoBehaviour
{
public Texture2D cursor;
public int cursorSizeX = 16; // default
public int cursorSizeY = 16; // default
// Use this for initialization
void Start ()
{
Object temp = Resources.Load("Textures/CR_Cursor (Custom)");
if (temp == null)
Debug.Log("Load Object Fail");
cursor = (Texture2D)Resources.Load("Textures/CR_Cursor (Custom)");
if (cursor == null)
Debug.Log("Load Cursor Fail");
Screen.showCursor = false;
}
// Update is called once per frame
void Update ()
{
GUI.DrawTexture(new Rect(Event.current.mousePosition.x - cursorSizeX / 2, Event.current.mousePosition.y - cursorSizeY / 2, cursorSizeX, cursorSizeY), cursor);
}
无论我在哪里实例化游标,我仍然得到NullReferenceException:对象引用未设置为对象的实例 Cursor.Update(),我缺少什么?
答案 0 :(得分:2)
你应该把这一行
GUI.DrawTexture(new Rect(Event.current.mousePosition.x - cursorSizeX / 2, Event.current.mousePosition.y - cursorSizeY / 2, cursorSizeX, cursorSizeY), cursor);
在OnGUI()内部,而不是在Update
中void OnGUI(){
GUI.DrawTexture(new Rect(Event.current.mousePosition.x - cursorSizeX / 2, Event.current.mousePosition.y - cursorSizeY / 2, cursorSizeX, cursorSizeY), cursor);
}
这是一个关于脚本生命周期的图表