Skip to content

Instantly share code, notes, and snippets.

@yangruihan
Created April 19, 2018 15:05
Show Gist options
  • Select an option

  • Save yangruihan/afffbed470c595ad976d26b07f535734 to your computer and use it in GitHub Desktop.

Select an option

Save yangruihan/afffbed470c595ad976d26b07f535734 to your computer and use it in GitHub Desktop.
Auto Bind Component
public static class GameObjectUtil
{
#if UNITY_EDITOR
public static GameObject FindGameObject(this GameObject obj, string name, bool includeInactive = true, bool showError = true)
{
Transform[] trs = obj.GetComponentsInChildren<Transform>(true);
foreach (Transform t in trs)
{
if (t.name == name)
{
return t.gameObject;
}
}
if (showError)
Debug.LogErrorFormat("ERROR: Cannot find {0}(type {1}) for {2}, please check!", name, obj.GetType(), obj);
return null;
}
public static bool BindComponent<T>(this GameObject obj, out T component,
string name, bool includeInactive = true, bool showError = true) where T : Component
{
component = null;
T temp = null;
Transform[] trs = obj.GetComponentsInChildren<Transform>(true);
foreach (Transform t in trs)
{
if (t.name == name)
{
var c = t.GetComponent<T>();
if (c != null)
{
if (temp != null)
{
if (showError)
Debug.LogErrorFormat("ERROR: There is more than 1 Component name of {0}, Please check it", name);
return false;
}
temp = c;
}
else
{
if (showError)
Debug.LogErrorFormat("ERROR: Cannot find {0} component in {1} Obj", typeof(T), name);
}
}
}
if (temp != null)
{
component = temp;
return true;
}
if (showError)
Debug.LogErrorFormat("ERROR: Cannot find {0}(type {1}) for {2}, please check!", name, obj.GetType(), obj);
return false;
}
#endif
}
using System;
using System.Collections;
using System.Collections.Generic;
using System.Reflection;
using System.Text;
using UnityEngine;
public class BindBase : MonoBehaviour
{
#if UNITY_EDITOR
protected Component FindComponentInObj(string name, Type type, bool showError = true)
{
var obj = gameObject.FindGameObject(name, showError: showError);
if (obj != null)
return obj.GetComponent(type);
return null;
}
protected bool BindComponent<T>(out T component, string name, bool showError = true) where T : Component
{
return gameObject.BindComponent(out component, name, showError: showError);
}
protected bool BindComponent<T>(out T component, bool showError = true) where T : Component
{
var c = GetComponentInChildren<T>();
if (c == null)
{
component = null;
if (showError)
Debug.LogErrorFormat("ERROR: Cannot find {0} component in {1} Obj's child", typeof(T), name);
return false;
}
component = c;
return true;
}
protected bool BindComponents<T>(out T[] components, bool showError = true) where T : Component
{
var c = GetComponentsInChildren<T>();
if (c == null)
{
components = null;
if (showError)
Debug.LogErrorFormat("ERROR: Cannot find {0} components in {1} Obj's child", typeof(T), name);
return false;
}
components = c;
return true;
}
[ContextMenu("Validate")]
public void ValidateMenu()
{
OnValidate();
}
protected void OnValidate()
{
}
[ContextMenu("Auto Bind")]
public void AutoBind()
{
StringBuilder sb = new StringBuilder();
sb.AppendLine("<color=Orange>Auto Bind Result:</color>\n---------------------\n");
int suc = 0;
int fail = 0;
Type type = this.GetType();
FieldInfo[] fields = type.GetFields(BindingFlags.Instance | BindingFlags.NonPublic | BindingFlags.Public);
foreach (var field in fields)
{
if (!IsTypeEqual(field.FieldType, typeof(Component)) || field.FieldType.IsArray)
continue;
var attrs = field.GetCustomAttributes(false);
if (field.IsPrivate)
{
if (ContainType(attrs, typeof(SerializeField)))
{
sb.AppendLine(BindFieldWithComponent(field, ref suc, ref fail));
}
}
else
{
if (!ContainType(attrs, typeof(HideInInspector)))
{
sb.AppendLine(BindFieldWithComponent(field, ref suc, ref fail));
}
}
}
sb.AppendLine(string.Format("\n<color=red>Total:</color>\nSucc Count {0}\nSkip Count {1}", suc, fail));
Debug.Log(sb.ToString());
}
private string BindFieldWithComponent(FieldInfo field, ref int suc, ref int fail)
{
var c = FindComponentInObj(field.Name, field.FieldType, showError: false);
if (c != null)
{
field.SetValue(this, c);
suc++;
return string.Format("<color=green>{0} \t----->\t {1}</color>", field.Name, GetObjUrl(c.transform, name));
}
else
{
fail++;
return string.Format("{0}\t\t\t\t\tSkip", field.Name);
}
}
public static string GetObjUrl(Transform tf, string rootName = "")
{
StringBuilder sb = new StringBuilder();
sb.Insert(0, tf.name);
Transform p = tf.parent;
while (p != null)
{
sb.Insert(0, p.name + "/");
p = p.parent;
if (p.name == rootName)
{
sb.Insert(0, p.name + "/");
break;
}
}
return sb.ToString();
}
public static bool ContainType(object[] arr, Type type)
{
foreach (var o in arr)
{
if (o.GetType() == type)
return true;
}
return false;
}
public static bool IsTypeEqual(Type type1, Type type2)
{
if (type1 == type2)
return true;
if (type1 == null || type2 == null)
return false;
return type2.IsAssignableFrom(type1);
}
#endif
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment