委託

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

public class MyAudioSource : MonoBehaviour{
    // 1、定義委託協議
    public delegate void MyBoolChangedDelegate(bool value);

    public MyBoolChangedDelegate OnBoolValueChanged;


    private bool boolValue;
    public bool BoolValue
    {
        get { return boolValue; }
        set
        {
            boolValue = value;
            if (OnBoolValueChanged != null)
            {
                // 4、調用委託
                OnBoolValueChanged.Invoke(boolValue);
            }
        }
    }

	
	void Start () {
        // 3、委託綁定
        OnBoolValueChanged = BoolChangeDelegate;
        
	}


    private void Update()
    {
        if (Input.GetMouseButtonDown(0))
        {
            BoolValue = !BoolValue;
        }
    }
    // 5、清空委託
    private void OnDestroy()
    {
        OnBoolValueChanged = null;
    }

    // 2、定義委託方法
    void BoolChangeDelegate(bool result)
    {
        print("Current bool result is: " + result);
    }
	
	
}

發佈了58 篇原創文章 · 獲贊 2 · 訪問量 2萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章