请编写一个脚本:在ugui实现类似fairygui里面的关联系统
时间: 2024-05-07 16:22:41 浏览: 10
以下是一个实现类似FairyGUI的关联系统的UGUI脚本:
```csharp
using UnityEngine;
using UnityEngine.UI;
using System.Collections.Generic;
public class UIRelationSystem : MonoBehaviour
{
public enum RelationType
{
None,
Left,
Right,
Top,
Bottom,
CenterX,
CenterY,
}
public class RelationInfo
{
public RelationType type;
public int targetId;
public int targetOffset;
public bool targetPercent;
}
private Dictionary<int, List<RelationInfo>> mRelations = new Dictionary<int, List<RelationInfo>>();
private RectTransform mRectTrans;
void Awake()
{
mRectTrans = GetComponent<RectTransform>();
}
public void AddRelation(RelationType type, int targetId, int targetOffset = 0, bool targetPercent = false)
{
if (!mRelations.ContainsKey(targetId))
{
mRelations.Add(targetId, new List<RelationInfo>());
}
List<RelationInfo> relations = mRelations[targetId];
RelationInfo info = new RelationInfo();
info.type = type;
info.targetId = targetId;
info.targetOffset = targetOffset;
info.targetPercent = targetPercent;
relations.Add(info);
}
public void RemoveRelation(int targetId)
{
if (mRelations.ContainsKey(targetId))
{
mRelations.Remove(targetId);
}
}
void Update()
{
foreach (KeyValuePair<int, List<RelationInfo>> kvp in mRelations)
{
RectTransform targetRectTrans = GetRectTransform(kvp.Key);
if (targetRectTrans != null)
{
foreach (RelationInfo info in kvp.Value)
{
switch (info.type)
{
case RelationType.Left:
SetLeft(targetRectTrans, info.targetOffset, info.targetPercent);
break;
case RelationType.Right:
SetRight(targetRectTrans, info.targetOffset, info.targetPercent);
break;
case RelationType.Top:
SetTop(targetRectTrans, info.targetOffset, info.targetPercent);
break;
case RelationType.Bottom:
SetBottom(targetRectTrans, info.targetOffset, info.targetPercent);
break;
case RelationType.CenterX:
SetCenterX(targetRectTrans, info.targetOffset, info.targetPercent);
break;
case RelationType.CenterY:
SetCenterY(targetRectTrans, info.targetOffset, info.targetPercent);
break;
}
}
}
}
}
private RectTransform GetRectTransform(int id)
{
GameObject go = GameObject.Find("UIObject" + id);
if (go != null)
{
return go.GetComponent<RectTransform>();
}
return null;
}
private void SetLeft(RectTransform target, int offset, bool percent)
{
float x = target.anchoredPosition.x - target.rect.width / 2 - mRectTrans.rect.width / 2;
if (percent)
{
x *= 100 / mRectTrans.rect.width;
}
mRectTrans.anchoredPosition = new Vector2(x - offset, mRectTrans.anchoredPosition.y);
}
private void SetRight(RectTransform target, int offset, bool percent)
{
float x = target.anchoredPosition.x + target.rect.width / 2 + mRectTrans.rect.width / 2;
if (percent)
{
x *= 100 / mRectTrans.rect.width;
}
mRectTrans.anchoredPosition = new Vector2(x + offset, mRectTrans.anchoredPosition.y);
}
private void SetTop(RectTransform target, int offset, bool percent)
{
float y = target.anchoredPosition.y + target.rect.height / 2 + mRectTrans.rect.height / 2;
if (percent)
{
y *= 100 / mRectTrans.rect.height;
}
mRectTrans.anchoredPosition = new Vector2(mRectTrans.anchoredPosition.x, y + offset);
}
private void SetBottom(RectTransform target, int offset, bool percent)
{
float y = target.anchoredPosition.y - target.rect.height / 2 - mRectTrans.rect.height / 2;
if (percent)
{
y *= 100 / mRectTrans.rect.height;
}
mRectTrans.anchoredPosition = new Vector2(mRectTrans.anchoredPosition.x, y - offset);
}
private void SetCenterX(RectTransform target, int offset, bool percent)
{
float x = target.anchoredPosition.x;
if (percent)
{
x *= 100 / mRectTrans.rect.width;
}
mRectTrans.anchoredPosition = new Vector2(x + offset, mRectTrans.anchoredPosition.y);
}
private void SetCenterY(RectTransform target, int offset, bool percent)
{
float y = target.anchoredPosition.y;
if (percent)
{
y *= 100 / mRectTrans.rect.height;
}
mRectTrans.anchoredPosition = new Vector2(mRectTrans.anchoredPosition.x, y + offset);
}
}
```
这个脚本可以作为UI元素上的组件添加。通过调用AddRelation方法来添加关系,通过传入类型、目标UI元素的ID、偏移量和是否按百分比来指定关系。例如:
```csharp
UIRelationSystem relation = myElement.AddComponent<UIRelationSystem>();
relation.AddRelation(UIRelationSystem.RelationType.Left, 1, 10, false);
relation.AddRelation(UIRelationSystem.RelationType.CenterY, 2, 0, true);
```
以上代码将在myElement上添加两个关系,一个是左边与ID为1的UI元素间隔10个像素,另一个是竖直居中于ID为2的UI元素。更新时,脚本将根据每个关系的类型和偏移量来计算新的位置。