660 lines
22 KiB
C#
660 lines
22 KiB
C#
using System;
|
||
using System.Collections;
|
||
using System.Collections.Generic;
|
||
using UnityEngine;
|
||
|
||
namespace BF
|
||
{
|
||
public enum GameObjectType
|
||
{
|
||
Default = 0,
|
||
UIObject = 1,
|
||
EffectObject = 2,
|
||
ModelObject = 3,
|
||
CharacterObject = 4,
|
||
TimelineObject = 5,
|
||
SpineUIObject = 6,
|
||
SpineMeshObject = 7,
|
||
}
|
||
[System.Serializable]
|
||
public struct GameObjectInfo
|
||
{
|
||
public string name;
|
||
public uint hashName;
|
||
public int objectType;
|
||
public UnityEngine.GameObject gameObject;
|
||
}
|
||
[ExecuteInEditMode]
|
||
[DisallowMultipleComponent]
|
||
public class PrefabHelper : MonoBehaviour
|
||
{
|
||
public List<GameObjectInfo> prefabList = new List<GameObjectInfo>();
|
||
public float PositionX { get; private set; }
|
||
public float PositionY { get; private set; }
|
||
public float PositionZ { get; private set; }
|
||
public GameObject GetGameObjectByName(string name)
|
||
{
|
||
int count = prefabList.Count;
|
||
for (int i = 0; i < count; i++)
|
||
{
|
||
if (prefabList[i].name == name)
|
||
{
|
||
return prefabList[i].gameObject;
|
||
}
|
||
}
|
||
return null;
|
||
}
|
||
|
||
public int GetListCount()
|
||
{
|
||
return prefabList.Count;
|
||
}
|
||
|
||
public uint GetHashNameByIndex(int index)
|
||
{
|
||
if (index >= 0 && index < prefabList.Count)
|
||
{
|
||
return prefabList[index].hashName;
|
||
}
|
||
return 0;
|
||
}
|
||
|
||
public int GetObjectTypeByIndex(int index)
|
||
{
|
||
if (index >= 0 && index < prefabList.Count)
|
||
{
|
||
return prefabList[index].objectType;
|
||
}
|
||
return 0;
|
||
}
|
||
|
||
public GameObject GetGameObjectByIndex(int index)
|
||
{
|
||
if (index >= 0 && index < prefabList.Count)
|
||
{
|
||
return prefabList[index].gameObject;
|
||
}
|
||
else if (index < 0)
|
||
{
|
||
return this.gameObject;
|
||
}
|
||
return null;
|
||
}
|
||
|
||
public void CacheAnchoredPosition(int index)
|
||
{
|
||
if (index >= 0 && index < prefabList.Count)
|
||
{
|
||
var rectTransform = prefabList[index].gameObject.transform as RectTransform;
|
||
PositionX = rectTransform.anchoredPosition.x;
|
||
PositionY = rectTransform.anchoredPosition.y;
|
||
}
|
||
else if (index < 0)
|
||
{
|
||
var rectTransform = this.transform as RectTransform;
|
||
PositionX = rectTransform.anchoredPosition.x;
|
||
PositionY = rectTransform.anchoredPosition.y;
|
||
}
|
||
}
|
||
|
||
public Vector3 GetAnchoredPosition(int index)
|
||
{
|
||
RectTransform rectTransform = null;
|
||
if (index >= 0 && index < prefabList.Count)
|
||
{
|
||
rectTransform = prefabList[index].gameObject.transform as RectTransform;
|
||
}
|
||
else if (index < 0)
|
||
{
|
||
rectTransform = this.transform as RectTransform;
|
||
}
|
||
if (rectTransform != null)
|
||
{
|
||
return rectTransform.anchoredPosition;
|
||
}
|
||
return Vector3.zero;
|
||
}
|
||
|
||
public void SetAnchoredPositionX(int index, float x)
|
||
{
|
||
RectTransform rectTransform = null;
|
||
if (index >= 0 && index < prefabList.Count)
|
||
{
|
||
rectTransform = prefabList[index].gameObject.transform as RectTransform;
|
||
}
|
||
else if (index < 0)
|
||
{
|
||
rectTransform = this.transform as RectTransform;
|
||
}
|
||
if (rectTransform != null)
|
||
{
|
||
rectTransform.anchoredPosition = new Vector2(x, rectTransform.anchoredPosition.y);
|
||
}
|
||
}
|
||
|
||
public void SetAnchoredPositionY(int index, float y)
|
||
{
|
||
RectTransform rectTransform = null;
|
||
if (index >= 0 && index < prefabList.Count)
|
||
{
|
||
rectTransform = prefabList[index].gameObject.transform as RectTransform;
|
||
}
|
||
else if (index < 0)
|
||
{
|
||
rectTransform = this.transform as RectTransform;
|
||
}
|
||
if (rectTransform != null)
|
||
{
|
||
rectTransform.anchoredPosition = new Vector2(rectTransform.anchoredPosition.x, y);
|
||
}
|
||
}
|
||
|
||
public void SetAnchoredPosition(int index, float x, float y)
|
||
{
|
||
RectTransform rectTransform = null;
|
||
if (index >= 0 && index < prefabList.Count)
|
||
{
|
||
rectTransform = prefabList[index].gameObject.transform as RectTransform;
|
||
}
|
||
else if (index < 0)
|
||
{
|
||
rectTransform = this.transform as RectTransform;
|
||
}
|
||
if (rectTransform != null)
|
||
{
|
||
rectTransform.anchoredPosition = new Vector2(x, y);
|
||
}
|
||
}
|
||
|
||
public void SetAnchorMin(int index, float x, float y)
|
||
{
|
||
RectTransform rectTransform = null;
|
||
if (index >= 0 && index < prefabList.Count)
|
||
{
|
||
rectTransform = prefabList[index].gameObject.transform as RectTransform;
|
||
}
|
||
else if (index < 0)
|
||
{
|
||
rectTransform = this.transform as RectTransform;
|
||
}
|
||
if (rectTransform != null)
|
||
{
|
||
rectTransform.anchorMin = new Vector2(x, y);
|
||
}
|
||
}
|
||
|
||
public void SetAnchorMax(int index, float x, float y)
|
||
{
|
||
RectTransform rectTransform = null;
|
||
if (index >= 0 && index < prefabList.Count)
|
||
{
|
||
rectTransform = prefabList[index].gameObject.transform as RectTransform;
|
||
}
|
||
else if (index < 0)
|
||
{
|
||
rectTransform = this.transform as RectTransform;
|
||
}
|
||
if (rectTransform != null)
|
||
{
|
||
rectTransform.anchorMax = new Vector2(x, y);
|
||
}
|
||
}
|
||
|
||
public void SetOffsetMin(int index, float x, float y)
|
||
{
|
||
RectTransform rectTransform = null;
|
||
if (index >= 0 && index < prefabList.Count)
|
||
{
|
||
rectTransform = prefabList[index].gameObject.transform as RectTransform;
|
||
}
|
||
else if (index < 0)
|
||
{
|
||
rectTransform = this.transform as RectTransform;
|
||
}
|
||
if (rectTransform != null)
|
||
{
|
||
rectTransform.offsetMin = new Vector2(x, y);
|
||
}
|
||
}
|
||
|
||
public void SetOffsetMax(int index, float x, float y)
|
||
{
|
||
RectTransform rectTransform = null;
|
||
if (index >= 0 && index < prefabList.Count)
|
||
{
|
||
rectTransform = prefabList[index].gameObject.transform as RectTransform;
|
||
}
|
||
else if (index < 0)
|
||
{
|
||
rectTransform = this.transform as RectTransform;
|
||
}
|
||
if (rectTransform != null)
|
||
{
|
||
rectTransform.offsetMax = new Vector2(x, y);
|
||
}
|
||
}
|
||
|
||
public void AddAnchoredPosition(int index, float x, float y)
|
||
{
|
||
RectTransform rectTransform = null;
|
||
if (index >= 0 && index < prefabList.Count)
|
||
{
|
||
rectTransform = prefabList[index].gameObject.transform as RectTransform;
|
||
}
|
||
else if (index < 0)
|
||
{
|
||
rectTransform = this.transform as RectTransform;
|
||
}
|
||
if (rectTransform != null)
|
||
{
|
||
Vector2 vec2 = rectTransform.anchoredPosition + new Vector2(x, y);
|
||
rectTransform.anchoredPosition = vec2;
|
||
}
|
||
}
|
||
|
||
public void SetSizeDelta(int index, float x, float y)
|
||
{
|
||
RectTransform rectTransform = null;
|
||
if (index >= 0 && index < prefabList.Count)
|
||
{
|
||
rectTransform = prefabList[index].gameObject.transform as RectTransform;
|
||
}
|
||
else if (index < 0)
|
||
{
|
||
rectTransform = this.transform as RectTransform;
|
||
}
|
||
if (rectTransform != null)
|
||
{
|
||
rectTransform.sizeDelta = new Vector2(x, y);
|
||
}
|
||
}
|
||
|
||
public void SetSizeDeltaX(int index, float x)
|
||
{
|
||
RectTransform rectTransform = null;
|
||
if (index >= 0 && index < prefabList.Count)
|
||
{
|
||
rectTransform = prefabList[index].gameObject.transform as RectTransform;
|
||
}
|
||
else if (index < 0)
|
||
{
|
||
rectTransform = this.transform as RectTransform;
|
||
}
|
||
if (rectTransform != null)
|
||
{
|
||
rectTransform.sizeDelta = new Vector2(x, rectTransform.sizeDelta.y);
|
||
}
|
||
}
|
||
|
||
public void SetSizeDeltaY(int index, float y)
|
||
{
|
||
RectTransform rectTransform = null;
|
||
if (index >= 0 && index < prefabList.Count)
|
||
{
|
||
rectTransform = prefabList[index].gameObject.transform as RectTransform;
|
||
}
|
||
else if (index < 0)
|
||
{
|
||
rectTransform = this.transform as RectTransform;
|
||
}
|
||
if (rectTransform != null)
|
||
{
|
||
rectTransform.sizeDelta = new Vector2(rectTransform.sizeDelta.x, y);
|
||
}
|
||
}
|
||
public void AddSizeDelta(int index, float x, float y)
|
||
{
|
||
RectTransform rectTransform = null;
|
||
if (index >= 0 && index < prefabList.Count)
|
||
{
|
||
rectTransform = prefabList[index].gameObject.transform as RectTransform;
|
||
}
|
||
else if (index < 0)
|
||
{
|
||
rectTransform = this.transform as RectTransform;
|
||
}
|
||
if (rectTransform != null)
|
||
{
|
||
Vector2 vec2 = rectTransform.sizeDelta + new Vector2(x, y);
|
||
rectTransform.sizeDelta = vec2;
|
||
}
|
||
}
|
||
|
||
public Vector2 GetSizeDelta(int index)
|
||
{
|
||
RectTransform rectTransform = null;
|
||
if (index >= 0 && index < prefabList.Count)
|
||
{
|
||
rectTransform = prefabList[index].gameObject.transform as RectTransform;
|
||
}
|
||
else if (index < 0)
|
||
{
|
||
rectTransform = this.transform as RectTransform;
|
||
}
|
||
if (rectTransform != null)
|
||
{
|
||
return rectTransform.sizeDelta;
|
||
}
|
||
return Vector2.zero;
|
||
}
|
||
|
||
public void CacheSizeDelt(int index)
|
||
{
|
||
RectTransform rectTransform = null;
|
||
if (index >= 0 && index < prefabList.Count)
|
||
{
|
||
rectTransform = prefabList[index].gameObject.transform as RectTransform;
|
||
}
|
||
else if (index < 0)
|
||
{
|
||
rectTransform = this.transform as RectTransform;
|
||
}
|
||
if (rectTransform != null)
|
||
{
|
||
PositionX = rectTransform.sizeDelta.x;
|
||
PositionY = rectTransform.sizeDelta.y;
|
||
}
|
||
else
|
||
{
|
||
PositionX = 0;
|
||
PositionY = 0;
|
||
}
|
||
}
|
||
|
||
public float GetRectWidth(int index)
|
||
{
|
||
RectTransform rectTransform = null;
|
||
if (index >= 0 && index < prefabList.Count)
|
||
{
|
||
rectTransform = prefabList[index].gameObject.transform as RectTransform;
|
||
return rectTransform.rect.width;
|
||
}
|
||
else if (index < 0)
|
||
{
|
||
rectTransform = this.transform as RectTransform;
|
||
return rectTransform.rect.width;
|
||
}
|
||
return 0.0f;
|
||
}
|
||
|
||
public float GetRectHeight(int index)
|
||
{
|
||
RectTransform rectTransform = null;
|
||
if (index >= 0 && index < prefabList.Count)
|
||
{
|
||
rectTransform = prefabList[index].gameObject.transform as RectTransform;
|
||
return rectTransform.rect.height;
|
||
}
|
||
else if (index < 0)
|
||
{
|
||
rectTransform = this.transform as RectTransform;
|
||
return rectTransform.rect.height;
|
||
}
|
||
return 0.0f;
|
||
}
|
||
|
||
public void SetPosition(int index, float x, float y, float z)
|
||
{
|
||
if (index >= 0 && index < prefabList.Count)
|
||
{
|
||
prefabList[index].gameObject.transform.position = new Vector3(x, y, z);
|
||
}
|
||
else if (index < 0)
|
||
{
|
||
this.transform.position = new Vector3(x, y, z);
|
||
}
|
||
}
|
||
|
||
public void AddPosition(int index, float x, float y, float z)
|
||
{
|
||
if (index >= 0 && index < prefabList.Count)
|
||
{
|
||
Transform transform = prefabList[index].gameObject.transform;
|
||
Vector3 vec3 = transform.position + new Vector3(x, y, z);
|
||
transform.position = vec3;
|
||
}
|
||
else if (index < 0)
|
||
{
|
||
Vector3 vec3 = this.transform.position + new Vector3(x, y, z);
|
||
this.transform.position = vec3;
|
||
}
|
||
}
|
||
|
||
public void SetLocalPosition(int index, float x, float y, float z)
|
||
{
|
||
if (index >= 0 && index < prefabList.Count)
|
||
{
|
||
prefabList[index].gameObject.transform.localPosition = new Vector3(x, y, z);
|
||
}
|
||
else if (index < 0)
|
||
{
|
||
this.transform.localPosition = new Vector3(x, y, z);
|
||
}
|
||
}
|
||
|
||
public void SetLocalPositionX(int index, float x)
|
||
{
|
||
if (index >= 0 && index < prefabList.Count)
|
||
{
|
||
var childTransform = prefabList[index].gameObject.transform;
|
||
childTransform.localPosition = new Vector3(x, childTransform.localPosition.y, childTransform.localPosition.z);
|
||
}
|
||
else if (index < 0)
|
||
{
|
||
this.transform.localPosition = new Vector3(x, this.transform.localPosition.y, this.transform.localPosition.z);
|
||
}
|
||
}
|
||
|
||
public void SetLocalPositionY(int index, float y)
|
||
{
|
||
if (index >= 0 && index < prefabList.Count)
|
||
{
|
||
var childTransform = prefabList[index].gameObject.transform;
|
||
childTransform.localPosition = new Vector3(childTransform.localPosition.x, y, childTransform.localPosition.z);
|
||
}
|
||
else if (index < 0)
|
||
{
|
||
this.transform.localPosition = new Vector3(this.transform.localPosition.x, y, this.transform.localPosition.z);
|
||
}
|
||
}
|
||
|
||
public void AddLocalPosition(int index, float x, float y, float z)
|
||
{
|
||
if (index >= 0 && index < prefabList.Count)
|
||
{
|
||
Transform transform = prefabList[index].gameObject.transform;
|
||
Vector3 vec3 = transform.localPosition + new Vector3(x, y, z);
|
||
transform.localPosition = vec3;
|
||
}
|
||
else if (index < 0)
|
||
{
|
||
Vector3 vec3 = this.transform.localPosition + new Vector3(x, y, z);
|
||
this.transform.localPosition = vec3;
|
||
}
|
||
}
|
||
|
||
// Lua层可以使用PositionX,PositionY,PositionZ,减少直接传递position的开销
|
||
public void CacheLocalPosition(int index)
|
||
{
|
||
if (index >= 0 && index < prefabList.Count)
|
||
{
|
||
var localPosition = prefabList[index].gameObject.transform.localPosition;
|
||
PositionX = localPosition.x;
|
||
PositionY = localPosition.y;
|
||
PositionZ = localPosition.z;
|
||
}
|
||
else if (index < 0)
|
||
{
|
||
PositionX = this.transform.localPosition.x;
|
||
PositionY = this.transform.localPosition.y;
|
||
PositionZ = this.transform.localPosition.z;
|
||
}
|
||
}
|
||
|
||
public void CachePosition(int index)
|
||
{
|
||
if (index >= 0 && index < prefabList.Count)
|
||
{
|
||
var position = prefabList[index].gameObject.transform.position;
|
||
PositionX = position.x;
|
||
PositionY = position.y;
|
||
PositionZ = position.z;
|
||
}
|
||
else if (index < 0)
|
||
{
|
||
PositionX = this.transform.position.x;
|
||
PositionY = this.transform.position.y;
|
||
PositionZ = this.transform.position.z;
|
||
}
|
||
}
|
||
|
||
public void CacheLocalScale(int index)
|
||
{
|
||
if (index >= 0 && index < prefabList.Count)
|
||
{
|
||
var localScale = prefabList[index].gameObject.transform.localScale;
|
||
PositionX = localScale.x;
|
||
PositionY = localScale.y;
|
||
PositionZ = localScale.z;
|
||
}
|
||
else if (index < 0)
|
||
{
|
||
PositionX = this.transform.localScale.x;
|
||
PositionY = this.transform.localScale.y;
|
||
PositionZ = this.transform.localScale.z;
|
||
}
|
||
}
|
||
|
||
public void SetEulerAngles(int index, float x, float y, float z)
|
||
{
|
||
if (index >= 0 && index < prefabList.Count)
|
||
{
|
||
prefabList[index].gameObject.transform.eulerAngles = new Vector3(x, y, z);
|
||
}
|
||
else if (index < 0)
|
||
{
|
||
this.transform.eulerAngles = new Vector3(x, y, z);
|
||
}
|
||
}
|
||
|
||
public void AddEulerAngles(int index, float x, float y, float z)
|
||
{
|
||
if (index >= 0 && index < prefabList.Count)
|
||
{
|
||
Transform transform = prefabList[index].gameObject.transform;
|
||
Vector3 vec3 = transform.eulerAngles + new Vector3(x, y, z);
|
||
transform.eulerAngles = vec3;
|
||
}
|
||
else if (index < 0)
|
||
{
|
||
Vector3 vec3 = this.transform.eulerAngles + new Vector3(x, y, z);
|
||
this.transform.eulerAngles = vec3;
|
||
}
|
||
}
|
||
|
||
public void SetLocalEulerAngles(int index, float x, float y, float z)
|
||
{
|
||
if (index >= 0 && index < prefabList.Count)
|
||
{
|
||
prefabList[index].gameObject.transform.localEulerAngles = new Vector3(x, y, z);
|
||
}
|
||
else if (index < 0)
|
||
{
|
||
this.transform.localEulerAngles = new Vector3(x, y, z);
|
||
}
|
||
}
|
||
|
||
public void AddLocalEulerAngles(int index, float x, float y, float z)
|
||
{
|
||
if (index >= 0 && index < prefabList.Count)
|
||
{
|
||
Transform transform = prefabList[index].gameObject.transform;
|
||
Vector3 vec3 = transform.localEulerAngles + new Vector3(x, y, z);
|
||
transform.localEulerAngles = vec3;
|
||
}
|
||
else if (index < 0)
|
||
{
|
||
Vector3 vec3 = this.transform.localEulerAngles + new Vector3(x, y, z);
|
||
this.transform.localEulerAngles = vec3;
|
||
}
|
||
}
|
||
public void SetLocalScale(int index, float x, float y, float z)
|
||
{
|
||
if (index >= 0 && index < prefabList.Count)
|
||
{
|
||
prefabList[index].gameObject.transform.localScale = new Vector3(x, y, z);
|
||
}
|
||
else if (index < 0)
|
||
{
|
||
this.transform.localScale = new Vector3(x, y, z);
|
||
}
|
||
}
|
||
|
||
public void AddLocalScale(int index, float x, float y, float z)
|
||
{
|
||
if (index >= 0 && index < prefabList.Count)
|
||
{
|
||
Transform transform = prefabList[index].gameObject.transform;
|
||
Vector3 vec3 = transform.localScale + new Vector3(x, y, z);
|
||
transform.localScale = vec3;
|
||
}
|
||
else if (index < 0)
|
||
{
|
||
Vector3 vec3 = this.transform.localScale + new Vector3(x, y, z);
|
||
this.transform.localScale = vec3;
|
||
}
|
||
}
|
||
|
||
public int GetRootPrefabIndex()
|
||
{
|
||
int index = -1;
|
||
for (int i = 0; i < prefabList.Count; i++)
|
||
{
|
||
if (prefabList[i].gameObject == this.gameObject)
|
||
{
|
||
index = i;
|
||
break;
|
||
}
|
||
}
|
||
return index;
|
||
}
|
||
|
||
public void SetScrollRectCellName(int index, int nameIndex)
|
||
{
|
||
if (index >= 0 && index < prefabList.Count)
|
||
{
|
||
prefabList[index].gameObject.name = StringConst.GetScrollRectCellName(nameIndex);
|
||
}
|
||
else if (index < 0)
|
||
{
|
||
this.gameObject.name = StringConst.GetScrollRectCellName(nameIndex);
|
||
}
|
||
}
|
||
|
||
public float GetlocalRotationZ(int index)
|
||
{
|
||
RectTransform rectTransform = null;
|
||
if (index >= 0 && index < prefabList.Count)
|
||
{
|
||
rectTransform = prefabList[index].gameObject.transform as RectTransform;
|
||
}
|
||
else if (index < 0)
|
||
{
|
||
rectTransform = this.transform as RectTransform;
|
||
}
|
||
if (rectTransform != null)
|
||
{
|
||
Vector3 vec3 = rectTransform.localRotation.eulerAngles;
|
||
return vec3.z;
|
||
}
|
||
return 0.0f;
|
||
}
|
||
|
||
void OnDestroy()
|
||
{
|
||
prefabList.Clear();
|
||
prefabList = null;
|
||
}
|
||
}
|
||
}
|