上海app网站建设,渭南网站建设价格,网页制作模板简单,百度数据在Unity中#xff0c;有时候我们在处理数据的时候会用到结构体定义一些Unity组件相关的数据成员#xff0c;并且需要在编辑器中拉取对象赋值。比如#xff1a;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;publ…在Unity中有时候我们在处理数据的时候会用到结构体定义一些Unity组件相关的数据成员并且需要在编辑器中拉取对象赋值。比如
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(){}
}上面案例中首先定义了个picData数据结构包含我们需要在场景中赋值的Img、text成员然后我们在主对象中声明了一个公共的结构体变量data。但是在Unity窗口中找不到结构体所定义的成员。
如何实现结构体定义的成员显示在窗口中
事实上它有两个必要条件
1、成员定义为公共成员
2、在定义结构体变量时要标记结构体为可序列化的。
最终定义如下
using System;[Serializable]
public struct picData
{public RawImage Img;public Text text;public string name;
}
在Unity窗口中我们就看到成员显示出来了 可根据需要拉取相关组件直接赋值。