简体   繁体   中英

Unity - Custom Inspector non-editable array

i'm creating a voxel game for fun while on vacation and i came across Custom Inspector and Editors, to make my life easier when creating new voxels.

The problem is: a simple voxel has a mesh composed of an array of 6 faces, each index corresponding to one direction, for example, the north face is the face of index 0, the upper face is index 4, and so on. But if i leave the inspector to show the array as it is, it's not guaranteed that each face will be placed on the right index.

My idea is to create the custom inspector and make it reorganize the array, the script of the editor would contain a new array "faces" (see the code below), that receive a face and a direction, and show in the inspector, making it possible . The current code is:

 using System; using UnityEditor; using UnityEngine; [CustomEditor(typeof(VoxelMesh))] public class VoxelMeshEditor: Editor { public enum DirectionEnum { North, East, South, West, Up, Down, Other, Special, All } [Serializable] public struct DirectionalFace { public DirectionEnum direction; public Face face; } [SerializeField] private DirectionalFace[] faces; private SerializedObject soEditor; private SerializedProperty spFaces; private void OnEnable() { soEditor = new SerializedObject(this); spFaces = soEditor.FindProperty("faces"); } public override void OnInspectorGUI() { base.OnInspectorGUI(); if (EditorGUILayout.PropertyField(spFaces, true)) { soEditor.ApplyModifiedProperties(); } } } 

The result is:

在此处输入图片说明

It creates an unchangeable array, i cannot edit it. How can i make it editable?

Thanks in advance.

尝试将DirectionalFace [] faces变量从private更改为public。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM