Skip to content

Commit

Permalink
整理代码
Browse files Browse the repository at this point in the history
  • Loading branch information
yinlong committed May 21, 2021
1 parent a53ebc0 commit 5f817c2
Show file tree
Hide file tree
Showing 6 changed files with 92 additions and 58 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,10 @@

namespace HotUnity.Editor
{
[InitializeOnLoadAttribute]
public class FixAddScriptButtonEditor
[InitializeOnLoad]
public class FixAddScriptButton
{
static FixAddScriptButtonEditor()
static FixAddScriptButton()
{
EditorApplication.hierarchyWindowItemOnGUI += HierarchyWindowItemOnGUI;
Selection.selectionChanged += OnSelectionChanged;
Expand Down
59 changes: 59 additions & 0 deletions UnityProject/Assets/HotUnity/Editor/HotFieldGUI.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
using System;
using System.Reflection;
using UnityEditor;
using UnityEngine;

namespace HotUnity.Editor
{
public static class HotFieldGUI
{
public static Type hotScriptType;

public static HotScriptAdapter.CacheInfo EditorDrawField(Type type, HotScriptAdapter.CacheInfo info)
{
var title = Helper.ToTitle(info.fieldName);
if (info.typeName == typeof(string).FullName)
{
info.stringValue = EditorGUILayout.TextField(title, info.stringValue);
}
else if (info.typeName == typeof(Vector3).FullName)
{
info.vector3Value = EditorGUILayout.Vector3Field(title, info.vector3Value);
}
else if (type.IsClass && typeof(Component).IsAssignableFrom(type))
{
info.componentValue = (Component)EditorGUILayout.ObjectField(title, info.componentValue, type);
}
else if (type.IsClass && hotScriptType.IsAssignableFrom(type))
{
var tempComp = (Component)EditorGUILayout.ObjectField(title, info.componentValue, typeof(HotScriptAdapter));
if (tempComp != null && tempComp is HotScriptAdapter
&& ((HotScriptAdapter)tempComp).targetClass == type.FullName)
{
info.componentValue = tempComp;
}
else
{
info.componentValue = null;
}
}
return info;
}


public static void RuntimeDrawField(FieldInfo fieldInfo, object obj)
{
var title = Helper.ToTitle(fieldInfo.Name);
if (fieldInfo.FieldType.FullName == typeof(string).FullName)
{
var value = EditorGUILayout.TextField(title, $"{fieldInfo.GetValue(obj)}");
fieldInfo.SetValue(obj, value);
}
else if (fieldInfo.FieldType.FullName == typeof(Vector3).FullName)
{
var value = EditorGUILayout.Vector3Field(title, (Vector3)fieldInfo.GetValue(obj));
fieldInfo.SetValue(obj, value);
}
}
}
}
11 changes: 11 additions & 0 deletions UnityProject/Assets/HotUnity/Editor/HotFieldGUI.cs.meta

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

57 changes: 4 additions & 53 deletions UnityProject/Assets/HotUnity/Editor/HotScriptAdapterEditor.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
using System;
using System.Collections.Generic;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using UnityEditor;
Expand Down Expand Up @@ -98,44 +97,11 @@ public override void OnInspectorGUI()
{
foreach (var f in fields)
{
RuntimeDrawField(f, target.targetObj);
HotFieldGUI.RuntimeDrawField(f, target.targetObj);
}
}
}

private HotScriptAdapter.CacheInfo EditorDrawInfo(Type type, HotScriptAdapter.CacheInfo info)
{
var title = Helper.ToTitle(info.fieldName);
if (info.typeName == typeof(string).FullName)
{
info.stringValue = EditorGUILayout.TextField(title, info.stringValue);
}
else if (info.typeName == typeof(Vector3).FullName)
{
info.vector3Value = EditorGUILayout.Vector3Field(title, info.vector3Value);
}
else if (type.IsClass && typeof(Component).IsAssignableFrom(type))
{
info.componentValue = (Component)EditorGUILayout.ObjectField(title, info.componentValue, type);
}
else if (type.IsClass &&
hotAssembly.GetType("HotUnity.HotScript").
ReflectionType.IsAssignableFrom(type))
{
var tempComp = (Component)EditorGUILayout.ObjectField(title, info.componentValue, typeof(HotScriptAdapter));
if (tempComp != null && tempComp is HotScriptAdapter
&& ((HotScriptAdapter)tempComp).targetClass == type.FullName)
{
info.componentValue = tempComp;
}
else
{
info.componentValue = null;
}
}
return info;
}

private bool EditorDrawField(FieldInfo fieldInfo, List<HotScriptAdapter.CacheInfo> infos)
{
var index = infos.FindIndex(a =>
Expand All @@ -158,7 +124,8 @@ private bool EditorDrawField(FieldInfo fieldInfo, List<HotScriptAdapter.CacheInf
info = infos[index];
}

info = EditorDrawInfo(fieldInfo.FieldType, info);
HotFieldGUI.hotScriptType = hotAssembly.GetType("HotUnity.HotScript").ReflectionType;
info = HotFieldGUI.EditorDrawField(fieldInfo.FieldType, info);

if (!infos[index].Equals(info))
{
Expand All @@ -167,21 +134,5 @@ private bool EditorDrawField(FieldInfo fieldInfo, List<HotScriptAdapter.CacheInf
}
return false;
}

private void RuntimeDrawField(FieldInfo fieldInfo, object obj)
{
var title = Helper.ToTitle(fieldInfo.Name);
if (fieldInfo.FieldType.FullName == typeof(string).FullName)
{
var value = EditorGUILayout.TextField(title, $"{fieldInfo.GetValue(obj)}");
fieldInfo.SetValue(obj, value);
}
else if (fieldInfo.FieldType.FullName == typeof(Vector3).FullName)
{
var value = EditorGUILayout.Vector3Field(title, (Vector3)fieldInfo.GetValue(obj));
fieldInfo.SetValue(obj, value);
}
}
}

}
17 changes: 15 additions & 2 deletions UnityProject/Assets/Resources/Test.prefab
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,7 @@ GameObject:
- component: {fileID: 3479655799002007851}
- component: {fileID: 7604365188659884831}
- component: {fileID: 1550526011755277691}
- component: {fileID: 1074195896740282542}
m_Layer: 0
m_Name: Button
m_TagString: Untagged
Expand Down Expand Up @@ -199,6 +200,18 @@ MonoBehaviour:
m_OnClick:
m_PersistentCalls:
m_Calls: []
--- !u!114 &1074195896740282542
MonoBehaviour:
m_ObjectHideFlags: 0
m_CorrespondingSourceObject: {fileID: 0}
m_PrefabInstance: {fileID: 0}
m_PrefabAsset: {fileID: 0}
m_GameObject: {fileID: 7060482671582365992}
m_Enabled: 1
m_EditorHideFlags: 0
m_Script: {fileID: 11500000, guid: 176d7da627249a5408814c66fde6ddcb, type: 3}
m_Name:
m_EditorClassIdentifier:
--- !u!1 &8064450304098585727
GameObject:
m_ObjectHideFlags: 0
Expand All @@ -210,7 +223,7 @@ GameObject:
- component: {fileID: 1037683787797942776}
- component: {fileID: 7238677793919117003}
- component: {fileID: 3642581602781590632}
- component: {fileID: 8114963186980429646}
- component: {fileID: 3675687587722284917}
m_Layer: 0
m_Name: Test
m_TagString: Untagged
Expand Down Expand Up @@ -302,7 +315,7 @@ MonoBehaviour:
vector3Value: {x: 0, y: 0, z: 0}
stringValue:
componentValue: {fileID: 7238677793919117003}
--- !u!114 &8114963186980429646
--- !u!114 &3675687587722284917
MonoBehaviour:
m_ObjectHideFlags: 0
m_CorrespondingSourceObject: {fileID: 0}
Expand Down

0 comments on commit 5f817c2

Please sign in to comment.