【Unity 15】 C# 结构体Struct的定义及使用

22 篇文章 9 订阅

PS:本系列笔记将会记录我此次在北京学习Unity开发的总体过程,方便后期写总结,笔记为日更。
笔记内容均为 自己理解,不保证每个都对。
C#笔记未按照难度排列

Part 1 结构体的定义:

在 C# 中,结构体是值类型数据结构。它使得一个单一变量可以存储各种数据类型的相关数据。struct 关键字用于创建结构体。结构体是用来代表一个记录。假设您想跟踪图书馆中书的动态。

定义结构体:必须用到Struct属性

基本格式为:

    struct 结构体名
    {
    	结构体属性
    	结构体方法(选填)
     }

例如定义一本书的结构体:

    struct Book
    {
        public string bookName;     //书名
        public string bookAuthor;   //作者
        public string bookSubject;  //科目
        public string bookID;       //编号

	    public void Print()
        {
            Console.WriteLine("bookID = {0}    bookName = {1}   bookAuthor = {2}   bookSubject = {3}", bookID, bookName, bookAuthor, bookSubject);
        }
     }

结构体的使用:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace FREE_TEST2_2019_08_08
{
    struct Book
    {
        public string bookName;     //书名
        public string bookAuthor;   //作者
        public string bookSubject;  //科目
        public string bookID;       //编号

        public void Print()
        {
            Console.WriteLine("bookID = {0}    bookName = {1}   bookAuthor = {2}   bookSubject = {3}", bookID, bookName, bookAuthor, bookSubject);
        }
    }


    class Program
    {
        static void Main(string[] args)
        {
            Book bookA;
            Book bookB;

            bookA.bookName = "The Magic Stone";
            bookA.bookAuthor = "JR";
            bookA.bookSubject = "Magic";
            bookA.bookID = "ISBC201908081510";

            bookB.bookName = "The Deathly Hallows";
            bookB.bookAuthor = "JR";
            bookB.bookSubject = "Magic";
            bookB.bookID = "ISBC201908081511";


            bookA.Print();
            bookB.Print();




            Console.ReadLine();
        }
    }
}

输出结果为在这里插入图片描述

Part 2结构体与类

在这里插入图片描述

若把刚刚书的结构体用 类表示 则代码为:

    class BookClass
    {
        public string bookName;     //书名
        public string bookAuthor;   //作者
        public string bookSubject;  //科目
        public string bookID;       //编号

        public BookClass()
        {

        }

        public BookClass(string bookName, string bookAuthor, string bookSubject, string bookID)
        {
            this.bookName = bookName;
            this.bookAuthor = bookAuthor;
            this.bookSubject = bookSubject;
            this.bookID = bookID;
        }

        public void Print()
        {
            Console.WriteLine("bookID = {0}    bookName = {1}   bookAuthor = {2}   bookSubject = {3}", bookID, bookName, bookAuthor, bookSubject);
        }
    }

区别:
结构体是值类型,类是引用类型
例如:

            BookClass bookOne = new BookClass("The Magic Stone", "JR", "Magic", "ISBC201908081510");
            BookClass bookTwo = new BookClass("The Deathly Hallows", "JR", "Magic", "ISBC201908081511");



            bookOne.Print();
            bookTwo.Print();


            Console.WriteLine("=============================================================");
            bookA = bookB;
            bookB.bookName = "The Order of the Phoenix";
            bookA.Print();

            Console.WriteLine();

            bookOne = bookTwo;
            bookTwo.bookName = "The Order of the Phoenix";
            bookOne.Print();

            Console.WriteLine("=============================================================");

测试结果为:
在这里插入图片描述
很明显发现
对结构体而言, 当赋值后 bookB发生改变时,bookA的属性不变
对类而言 ,当赋值后bookTwo发生改变时,booKOne的属性随着改变

  • 15
    点赞
  • 60
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
### 回答1: 在Unity编辑器中显示自定义结构体,需要使用Unity的PropertyDrawer功能。您可以通过创建一个继承自PropertyDrawer的类并使用CustomPropertyDrawer特性来实现。 您可以在代码中这样实现: ``` using UnityEngine; using UnityEditor; [CustomPropertyDrawer(typeof(YourCustomStruct))] public class YourCustomStructDrawer : PropertyDrawer { public override void OnGUI(Rect position, SerializedProperty property, GUIContent label) { // Your custom GUI code here } } ``` 这样,在Unity编辑器中,您就可以看到您自定义结构体以符合您的需求的方式显示了。 ### 回答2: 在Unity编辑器中显示自定义struct可以通过创建自定义的Editor类来实现。以下是一个基本的示例: 首先,我们需要创建一个名为"CustomStruct"的自定义struct,包含我们希望显示的成员变量和方法。 ```csharp [System.Serializable] public struct CustomStruct { public int intValue; public float floatValue; public string stringValue; public void CustomMethod() { // 自定义方法的实现 } } ``` 然后,我们创建一个名为"CustomStructEditor"的自定义Editor类,并继承自UnityEditor类库中的Editor类。 ```csharp using UnityEditor; using UnityEngine; [CustomEditor(typeof(MonoBehaviour))] public class CustomStructEditor : Editor { private SerializedProperty intValueProp; private SerializedProperty floatValueProp; private SerializedProperty stringValueProp; private void OnEnable() { intValueProp = serializedObject.FindProperty("intValue"); floatValueProp = serializedObject.FindProperty("floatValue"); stringValueProp = serializedObject.FindProperty("stringValue"); } public override void OnInspectorGUI() { serializedObject.Update(); EditorGUILayout.PropertyField(intValueProp); EditorGUILayout.PropertyField(floatValueProp); EditorGUILayout.PropertyField(stringValueProp); serializedObject.ApplyModifiedProperties(); } } ``` 最后,将该自定义Editor类与含有自定义struct的MonoBehaviour脚本关联起来。例如,我们可以将CustomStructEditor类与一个名为"CustomBehaviour"的脚本关联: ```csharp [CustomEditor(typeof(CustomBehaviour))] public class CustomBehaviourEditor : Editor { public override void OnInspectorGUI() { base.OnInspectorGUI(); CustomBehaviour customBehaviour = (CustomBehaviour)target; EditorGUILayout.Space(); if (GUILayout.Button("Call Custom Method")) { customBehaviour.customStruct.CustomMethod(); } } } ``` 这样,在Unity编辑器中,我们可以像显示其他属性一样显示我们自定义struct,同时还可以调用自定义方法。 希望这个简单的示例可以帮到您。 ### 回答3: 在Unity编辑器中显示自定义struct结构体)数据类型,需要通过自定义Unity的自定义编辑器(Custom Editor)来实现。 首先,我们需要在Unity项目中创建一个C#脚本,并在脚本中定义我们需要显示的struct数据类型。例如,我们可以创建一个名为"CustomStruct.cs"的脚本,在其中定义了一个名为"CustomStruct"的struct数据类型。 接下来,我们可以创建一个名为"CustomStructEditor.cs"的自定义编辑器脚本。在该脚本中,我们可以使用Unity的GUI系统来自定义显示struct数据类型的方式。我们可以使用GUILayout或者EditorGUILayout等GUI布局函数来创建自定义的Inspector面板。 在CustomStructEditor.cs中,我们可以重写OnInspectorGUI函数,并在该函数中编写我们希望在Inspector面板中显示的内容。我们可以使用SerializedProperty来访问和修改struct中的成员变量。 例如,我们可以使用GUILayout.Label函数来显示一些文本,使用SerializedProperty.FindPropertyRelative函数来获取struct中的成员变量,并使用 EditorGUILayout.PropertyField 函数来显示每个成员变量。 最后,在CustomStruct.cs脚本的声明前面添加"[CustomEditor(typeof(CustomStructEditor))]"来告诉Unity,我们希望使用我们自定义的编辑器来显示CustomStruct。 总结,通过使用定义编辑器,我们可以在Unity编辑器中显示自定义struct数据类型。自定义编辑器使我们能够按照我们的需求来显示和修改struct的成员变量,使开发工作更加高效和灵活。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值