之前公司寫UNITY是用 UNITY SCRIPT,專案越寫越大,總感覺有些不夠嚴謹。
下次專案預定改成C#
C#有一個很好用的機制 delegate
就當作學習心得記錄下來
主程式 拖到場景的物件中
using UnityEngine;
using System.Collections;
public class test : MonoBehaviour {
public delegate string ProcessDelegate(string s1, string s2);
ProcessDelegate pd;
// Use this for initialization
void Start () {
pd = new ProcessDelegate(new delegateEx1().Process);
}
// Update is called once per frame
void Update ()
{
Debug.Log(pd("Hello", "World"));
}
}
code delegateEx1 如下
using UnityEngine;
using System.Collections;
public class delegateEx1 {
public string Process(string s1,string s2)
{
return s1 + s2;
}
}
結果會在 Console視窗中不斷顯示 HelloWorld
另一種調用的方式
using UnityEngine;
using System.Collections;
public class test : MonoBehaviour {
public delegate string ProcessDelegate(string s1, string s2);
ProcessDelegate pd;
// Use this for initialization
void Start () {
//pd = new ProcessDelegate(new delegateEx1().Process);
ReceiveDelegateArgsFunc(new ProcessDelegate(new delegateEx1().Process));
}
// Update is called once per frame
void Update ()
{
//Debug.Log(pd("Hello", "World"));
}
public void ReceiveDelegateArgsFunc(ProcessDelegate func)
{
string str = func("good","test");
Debug.Log(str);
}
}
結果會在 Console視窗中顯示 goodtest
沒有留言:
張貼留言