unity学习笔记-实例化instance

实例化Test1

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class Test1 : MonoBehaviour
{
    public bool p = true;
    //实例化步骤1
    public static Test1 instance;
    void Awake()
    {
        //实例化步骤2
        instance = this;
    }

    void Update()
    {
        if (p == false)
        {
            print("p-");
        }
    }
}

引用Test1

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class Test3 : MonoBehaviour
{
    //引用步骤1
    private Test1 Test1;
    void Start()
    {
        //引用步骤2
        Test1 = Test1.instance;
    }

    void Update()
    {
        //修改Test1变量
        Test1.p = false;
    }
}

结果

猜你喜欢

转载自blog.csdn.net/m0_73841296/article/details/131189050
今日推荐