上海網(wǎng)站建設(shè)設(shè)計(jì)公司排名無(wú)錫百度推廣開戶
在Unity中,有時(shí)候我們?cè)谔幚頂?shù)據(jù)的時(shí)候會(huì)用到結(jié)構(gòu)體定義一些Unity組件相關(guān)的數(shù)據(jù)成員,并且需要在編輯器中拉取對(duì)象賦值。比如:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;public struct picData
{public RawImage Img;public Text text;public string name;
}public class StructTest : MonoBehaviour
{public picData data;// Start is called before the first frame updatevoid Start(){}// Update is called once per framevoid Update(){}
}
上面案例中,首先定義了個(gè)picData數(shù)據(jù)結(jié)構(gòu),包含我們需要在場(chǎng)景中賦值的Img、text成員,然后我們?cè)谥鲗?duì)象中聲明了一個(gè)公共的結(jié)構(gòu)體變量data。但是在Unity窗口中找不到結(jié)構(gòu)體所定義的成員。
如何實(shí)現(xiàn)結(jié)構(gòu)體定義的成員顯示在窗口中?
事實(shí)上,它有兩個(gè)必要條件:
1、成員定義為公共成員;
2、在定義結(jié)構(gòu)體變量時(shí)要標(biāo)記結(jié)構(gòu)體為可序列化的。
最終定義如下:
using System;[Serializable]
public struct picData
{public RawImage Img;public Text text;public string name;
}
在Unity窗口中我們就看到成員顯示出來(lái)了:
?
可根據(jù)需要拉取相關(guān)組件直接賦值。