277 lines
8.3 KiB
C#
277 lines
8.3 KiB
C#
using System.Collections;
|
||
using System.Collections.Generic;
|
||
using UnityEngine;
|
||
using UObject = UnityEngine.Object;
|
||
using System;
|
||
|
||
public static class GameObjectExtension
|
||
{
|
||
/// <summary>
|
||
/// ÏÔʾÒþ²Ø×ÔÉí
|
||
/// </summary>
|
||
/// <param name="self"></param>
|
||
/// <param name="_display"></param>
|
||
public static void SetMeshRendererActive(this GameObject self, bool _display)
|
||
{
|
||
MeshRenderer meshRenderer = self.GetComponent<MeshRenderer>();
|
||
if (meshRenderer != null)
|
||
{
|
||
meshRenderer.enabled = _display;
|
||
}
|
||
}
|
||
|
||
/// <summary>
|
||
/// MRÏÔʾÒþ²Ø
|
||
/// </summary>
|
||
/// <param name="self"></param>
|
||
/// <param name="_display"></param>
|
||
/// <param name="_tex"></param>
|
||
public static void SetMeshRendererActive(this GameObject self, bool _display, Texture _tex)
|
||
{
|
||
MeshRenderer meshRenderer = self.GetComponent<MeshRenderer>();
|
||
if (meshRenderer != null)
|
||
{
|
||
meshRenderer.enabled = _display;
|
||
meshRenderer.material.SetTexture("_MainTex", _tex);
|
||
}
|
||
}
|
||
|
||
/// <summary>
|
||
/// MRÏÔʾÒþ²Ø
|
||
/// </summary>
|
||
/// <param name="self"></param>
|
||
/// <param name="_display"></param>
|
||
/// <param name="_tex"></param>
|
||
public static void SetMeshRendererActive(this GameObject self, bool _display, Material _mat)
|
||
{
|
||
MeshRenderer meshRenderer = self.GetComponent<MeshRenderer>();
|
||
if (meshRenderer != null)
|
||
{
|
||
meshRenderer.enabled = _display;
|
||
meshRenderer.material = _mat;
|
||
}
|
||
}
|
||
#region Instantiate
|
||
|
||
public static UObject Instantiate(this GameObject self, UObject ori)
|
||
{
|
||
UObject clone = UObject.Instantiate(ori);
|
||
Debug.Log("Clone object" + clone.name);
|
||
return clone;
|
||
}
|
||
|
||
#endregion
|
||
|
||
#region Transform
|
||
|
||
public static void ChildDo(this Transform self, Action<GameObject> _call)
|
||
{
|
||
for (int i = 0; i < self.childCount; i++)
|
||
{
|
||
_call?.Invoke(self.GetChild(i).gameObject);
|
||
}
|
||
}
|
||
|
||
#endregion
|
||
|
||
#region Destroy
|
||
|
||
public static void Destroy(this GameObject self, UObject obj)
|
||
{
|
||
GameObject.Destroy(obj);
|
||
}
|
||
|
||
#endregion
|
||
|
||
public static void CheckComponent(this GameObject self, params System.Type[] _components)
|
||
{
|
||
for (int i = 0; i < _components.Length; i++)
|
||
{
|
||
if (self.GetComponent(_components[i]) == null)
|
||
{
|
||
self.AddComponent(_components[i]);
|
||
}
|
||
}
|
||
}
|
||
|
||
/// <summary>
|
||
/// ¸´ÖÆTransform
|
||
/// </summary>
|
||
/// <param name="self"></param>
|
||
/// <param name="_other"></param>
|
||
/// <param name="_copyPosition"></param>
|
||
/// <param name="_copyRotation"></param>
|
||
/// <param name="_copyScale"></param>
|
||
public static void CopyTransform(this GameObject self, GameObject _other, bool _copyPosition = true, bool _copyRotation = false, bool _copyScale = false)
|
||
{
|
||
if (_other == null) return;
|
||
if (_copyPosition)
|
||
self.transform.position = _other.transform.position;
|
||
if (_copyRotation)
|
||
self.transform.rotation = _other.transform.rotation;
|
||
if (_copyScale)
|
||
self.transform.localScale = _other.transform.localScale;
|
||
}
|
||
|
||
/// <summary>
|
||
/// ȱÏݹÊÕϳõʼ»¯
|
||
/// </summary>
|
||
/// <param name="self"></param>
|
||
public static void TryInitIssueOptional(this GameObject self, AttrData data/*DefectDetails details*/, bool _addComponent = true)
|
||
{
|
||
Optional optional = self.GetComponent<Optional>();
|
||
if (optional == null)
|
||
{
|
||
if (_addComponent)
|
||
optional = self.AddComponent<Optional>();
|
||
}
|
||
|
||
if (!_addComponent && optional == null) return;
|
||
optional.IssueInit(data);
|
||
|
||
ErrorModel errorModel = self.GetComponent<ErrorModel>();
|
||
if (errorModel != null)
|
||
{
|
||
optional.typeName = errorModel.modelType.ToString();
|
||
}
|
||
else
|
||
Debug.Log(self.name);
|
||
}
|
||
|
||
/// <summary>
|
||
/// ȱÏݹÊÕϳõʼ»¯
|
||
/// </summary>
|
||
/// <param name="self"></param>
|
||
public static void TryInitIssueOptional(this GameObject self, AttrData data/* DefectDetails details*/, Texture2D _texture2D,bool _inactive,bool _addComponent = true)
|
||
{
|
||
Optional optional = self.GetComponent<Optional>();
|
||
if (optional == null)
|
||
{
|
||
if (_addComponent)
|
||
optional = self.AddComponent<Optional>();
|
||
}
|
||
|
||
if (!_addComponent && optional == null) return;
|
||
optional.IssueInit(data, _texture2D, _inactive);
|
||
|
||
ErrorModel errorModel = self.GetComponent<ErrorModel>();
|
||
if (errorModel != null)
|
||
{
|
||
optional.typeName = errorModel.modelType.ToString();
|
||
}
|
||
else
|
||
Debug.Log(self.name);
|
||
}
|
||
|
||
/// <summary>
|
||
/// ·ÇȱÏÝÕý³£³õʼ»¯
|
||
/// </summary>
|
||
/// <param name="self"></param>
|
||
/// <param name="_addComponent"></param>
|
||
public static void TryInitNormalOptional(this GameObject self, bool _addComponent = true)
|
||
{
|
||
Optional optional = self.GetComponent<Optional>();
|
||
if (optional == null)
|
||
{
|
||
if (_addComponent)
|
||
optional = self.AddComponent<Optional>();
|
||
}
|
||
|
||
if (!_addComponent && optional == null) return;
|
||
optional.NormalInit();
|
||
|
||
ErrorModel errorModel = self.GetComponent<ErrorModel>();
|
||
if (errorModel != null)
|
||
{
|
||
optional.typeName = errorModel.modelType.ToString();
|
||
}
|
||
else
|
||
Debug.Log(self.name);
|
||
}
|
||
|
||
/// <summary>
|
||
/// ·ÇȱÏÝÕý³£³õʼ»¯
|
||
/// </summary>
|
||
/// <param name="self"></param>
|
||
/// <param name="_addComponent"></param>
|
||
public static void TryInitNormalOptional(this GameObject self, Texture2D _texture2D,bool _addComponent = true)
|
||
{
|
||
Optional optional = self.GetComponent<Optional>();
|
||
if (optional == null)
|
||
{
|
||
if (_addComponent)
|
||
optional = self.AddComponent<Optional>();
|
||
}
|
||
|
||
if (!_addComponent && optional == null) return;
|
||
optional.NormalInit(_texture2D);
|
||
|
||
ErrorModel errorModel = self.GetComponent<ErrorModel>();
|
||
if (errorModel != null)
|
||
{
|
||
optional.typeName = errorModel.modelType.ToString();
|
||
}
|
||
else
|
||
Debug.Log(self.name);
|
||
}
|
||
|
||
/// <summary>
|
||
/// ²éÕÒÂú×ãÌõ¼þµÄ×ÓÎïÌå
|
||
/// </summary>
|
||
/// <typeparam name="T"></typeparam>
|
||
/// <param name="self"></param>
|
||
/// <param name="_call"></param>
|
||
/// <returns></returns>
|
||
public static T TryGetComponintInChildren<T>(this GameObject self, Predicate<T> _call)
|
||
{
|
||
T[] childs = self.GetComponentsInChildren<T>();
|
||
T res = Array.Find(childs, _call);
|
||
return res;
|
||
}
|
||
|
||
/// <summary>
|
||
/// ²éÕÒËùÓÐÂú×ãÌõ¼þµÄ×ÓÎïÌå
|
||
/// </summary>
|
||
/// <typeparam name="T"></typeparam>
|
||
/// <param name="self"></param>
|
||
/// <param name="_call"></param>
|
||
/// <returns></returns>
|
||
public static T[] TryGetComponintsInChildren<T>(this GameObject self, Predicate<T> _call)
|
||
{
|
||
T[] childs = self.GetComponentsInChildren<T>();
|
||
T[] res = Array.FindAll(childs, _call);
|
||
return res;
|
||
}
|
||
|
||
/// <summary>
|
||
/// »ñÈ¡Ãû×Ö°üº¬Ìض¨×Ö·û´®µÄ×ÓÎïÌå
|
||
/// </summary>
|
||
/// <param name="self"></param>
|
||
/// <param name="_nameContains"></param>
|
||
/// <returns></returns>
|
||
public static GameObject GetChildByNameContains(this GameObject self, string _nameContains, bool includeInactive = false)
|
||
{
|
||
GameObject res = null;
|
||
Array.ForEach(self.GetComponentsInChildren<Transform>(includeInactive), tra =>
|
||
{
|
||
if (tra != self.transform && tra.name.Contains(_nameContains)) { res = tra.gameObject; return; }
|
||
});
|
||
return res;
|
||
}
|
||
|
||
/// <summary>
|
||
/// »ñÈ¡Ãû×Ö°üº¬Ìض¨×Ö·û´®µÄ×ÓÎïÌå
|
||
/// </summary>
|
||
/// <param name="self"></param>
|
||
/// <param name="_nameContains"></param>
|
||
/// <returns></returns>
|
||
public static List<GameObject> GetChildsByNameContains(this GameObject self, string _nameContains,bool includeInactive = false)
|
||
{
|
||
List<GameObject> res = new List<GameObject>();
|
||
Array.ForEach(self.GetComponentsInChildren<Transform>(includeInactive), tra =>
|
||
{
|
||
if (tra != self.transform && tra.name.Contains(_nameContains)) res.Add(tra.gameObject);
|
||
});
|
||
return res;
|
||
}
|
||
} |