using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UObject = UnityEngine.Object;
using System;
public static class GameObjectExtension
{
///
/// 显示隐藏自身
///
///
///
public static void SetMeshRendererActive(this GameObject self, bool _display)
{
MeshRenderer meshRenderer = self.GetComponent();
if (meshRenderer != null)
{
meshRenderer.enabled = _display;
}
}
///
/// MR显示隐藏
///
///
///
///
public static void SetMeshRendererActive(this GameObject self, bool _display, Texture _tex)
{
MeshRenderer meshRenderer = self.GetComponent();
if (meshRenderer != null)
{
meshRenderer.enabled = _display;
meshRenderer.material.SetTexture("_MainTex", _tex);
}
}
///
/// MR显示隐藏
///
///
///
///
public static void SetMeshRendererActive(this GameObject self, bool _display, Material _mat)
{
MeshRenderer meshRenderer = self.GetComponent();
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 _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]);
}
}
}
///
/// 复制Transform
///
///
///
///
///
///
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;
}
///
/// 缺陷故障初始化
///
///
public static void TryInitIssueOptional(this GameObject self, AttrData data/*DefectDetails details*/, bool _addComponent = true)
{
Optional optional = self.GetComponent();
if (optional == null)
{
if (_addComponent)
optional = self.AddComponent();
}
if (!_addComponent && optional == null) return;
optional.IssueInit(data);
ErrorModel errorModel = self.GetComponent();
if (errorModel != null)
{
optional.typeName = errorModel.modelType.ToString();
}
else
Debug.Log(self.name);
}
///
/// 缺陷故障初始化
///
///
public static void TryInitIssueOptional(this GameObject self, AttrData data/* DefectDetails details*/, Texture2D _texture2D,bool _inactive,bool _addComponent = true)
{
Optional optional = self.GetComponent();
if (optional == null)
{
if (_addComponent)
optional = self.AddComponent();
}
if (!_addComponent && optional == null) return;
optional.IssueInit(data, _texture2D, _inactive);
ErrorModel errorModel = self.GetComponent();
if (errorModel != null)
{
optional.typeName = errorModel.modelType.ToString();
}
else
Debug.Log(self.name);
}
///
/// 非缺陷正常初始化
///
///
///
public static void TryInitNormalOptional(this GameObject self, bool _addComponent = true)
{
Optional optional = self.GetComponent();
if (optional == null)
{
if (_addComponent)
optional = self.AddComponent();
}
if (!_addComponent && optional == null) return;
optional.NormalInit();
ErrorModel errorModel = self.GetComponent();
if (errorModel != null)
{
optional.typeName = errorModel.modelType.ToString();
}
else
Debug.Log(self.name);
}
///
/// 非缺陷正常初始化
///
///
///
public static void TryInitNormalOptional(this GameObject self, Texture2D _texture2D,bool _addComponent = true)
{
Optional optional = self.GetComponent();
if (optional == null)
{
if (_addComponent)
optional = self.AddComponent();
}
if (!_addComponent && optional == null) return;
optional.NormalInit(_texture2D);
ErrorModel errorModel = self.GetComponent();
if (errorModel != null)
{
optional.typeName = errorModel.modelType.ToString();
}
else
Debug.Log(self.name);
}
///
/// 查找满足条件的子物体
///
///
///
///
///
public static T TryGetComponintInChildren(this GameObject self, Predicate _call)
{
T[] childs = self.GetComponentsInChildren();
T res = Array.Find(childs, _call);
return res;
}
///
/// 查找所有满足条件的子物体
///
///
///
///
///
public static T[] TryGetComponintsInChildren(this GameObject self, Predicate _call)
{
T[] childs = self.GetComponentsInChildren();
T[] res = Array.FindAll(childs, _call);
return res;
}
///
/// 获取名字包含特定字符串的子物体
///
///
///
///
public static GameObject GetChildByNameContains(this GameObject self, string _nameContains, bool includeInactive = false)
{
GameObject res = null;
Array.ForEach(self.GetComponentsInChildren(includeInactive), tra =>
{
if (tra != self.transform && tra.name.Contains(_nameContains)) { res = tra.gameObject; return; }
});
return res;
}
///
/// 获取名字包含特定字符串的子物体
///
///
///
///
public static List GetChildsByNameContains(this GameObject self, string _nameContains,bool includeInactive = false)
{
List res = new List();
Array.ForEach(self.GetComponentsInChildren(includeInactive), tra =>
{
if (tra != self.transform && tra.name.Contains(_nameContains)) res.Add(tra.gameObject);
});
return res;
}
}