/// <summary> /// 将读到的property数组转为list /// </summary> private void TurnPropertyArray2List(SerializedProperty refList) { if (refList is null) { Log.Error("refList is null"); return; } if (_list is null) { _list = new List <InspectorSerializObjct>(); } WindowInspectorObj obj = null; var cnt = refList.arraySize; for (int i = 0; i < cnt; i++) { var sObj = refList.GetArrayElementAtIndex(i); var go = sObj.FindPropertyRelative("go"); var type = sObj.FindPropertyRelative("type").stringValue; var name = sObj.FindPropertyRelative("name").stringValue; if (go is null || string.IsNullOrEmpty(type) || string.IsNullOrEmpty(name)) { continue; } var serialObj = new WindowInspectorObj(go.objectReferenceValue as GameObject, type, name); var index = GetSelectedIndex(serialObj.Type, GetGameobjetsCompTypes(serialObj.GameObj)); var currSelected = Selection.objects[0]; var temp = currSelected as GameObject; var inspectObj = new InspectorSerializObjct() { _serializObject = serialObj, _selectTypeIdx = index, _objectNodePath = UITools.GetGameObjectChildPath(serialObj.GameObj, temp), }; _list.Add(inspectObj); } }
/// <summary> /// 绘制引用相关按钮 /// </summary> private void DrawReferenceButton() { EditorGUILayout.BeginVertical(); EditorGUILayout.BeginHorizontal(); if (GUILayout.Button(" + ")) { var obj = new InspectorSerializObjct(); obj._selectTypeIdx = 0; _list.Add(obj); } if (GUILayout.Button(" - ")) { var cnt = _list.Count; if (cnt == 0) { return; } _list.RemoveAt(cnt - 1); } if (GUILayout.Button("Save Reference")) { //重新写入到propertyList var cnt = _list.Count; if (_referenceList is null) { return; } _referenceList.ClearArray(); _referenceList.arraySize = cnt; InspectorSerializObjct sObj; for (int i = 0; i < cnt; i++) { sObj = _list[i]; _referenceList.InsertArrayElementAtIndex(i); var comp = _referenceList.GetArrayElementAtIndex(i); var go = comp.FindPropertyRelative("go"); var type = comp.FindPropertyRelative("type"); var name = comp.FindPropertyRelative("name"); go.objectReferenceValue = sObj._serializObject.GameObj; type.stringValue = sObj._serializObject.Type; name.stringValue = sObj._serializObject.Name; } //写入类名 var className = serializedObject.FindProperty("_className"); className.stringValue = _className; //Undo.RecordObject( target, "tar changed" ); serializedObject.ApplyModifiedProperties(); } if (GUILayout.Button("Clear Reference")) { _referenceList?.ClearArray(); _list?.Clear(); serializedObject.ApplyModifiedProperties(); } EditorGUILayout.EndHorizontal(); EditorGUILayout.EndVertical(); }