Gameplay Ingredients是一组用于 Unity 游戏的运行时和编辑器工具:一组脚本的集合,可在制作游戏和原型时简化简单的任务。
您最多选择25个主题 主题必须以中文或者字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

59 行
1.9 KiB

using NaughtyAttributes;
using UnityEngine;
namespace GameplayIngredients.Actions
{
[AddComponentMenu(ComponentMenu.actionsPath + "Attach To Object Action")]
[Callable("Game Objects", "Actions/ic-action-attach.png")]
public class AttachToObjectAction : ActionBase
{
public enum Action
{
Attach,
Detach,
ToggleAttachment
}
public Action action = Action.Attach;
[ReorderableList]
public GameObject[] objectsToAttach;
public bool AttachInstigator = false;
public GameObject parentObject;
public bool KeepScale;
public override void Execute(GameObject instigator = null)
{
if (parentObject == null)
{
Debug.LogWarning("No Object to attach to.");
return;
}
if (objectsToAttach != null)
{
foreach (var obj in objectsToAttach)
DoAttach(obj, parentObject, action, KeepScale);
}
if (AttachInstigator && instigator != null)
DoAttach(instigator, parentObject, action, KeepScale);
}
static void DoAttach(GameObject attachment, GameObject parent, Action action, bool keepScale)
{
if(action == Action.Attach || (action == Action.ToggleAttachment && attachment.transform.parent != parent.transform))
{
attachment.transform.parent = parent.transform;
}
else if(action == Action.Detach || (action == Action.ToggleAttachment && attachment.transform.parent == parent.transform))
{
attachment.transform.parent = null;
}
}
public override string GetDefaultName()
{
return $"{action} : {(AttachInstigator?"instigator":"objects")} to '{parentObject?.name}'";
}
}
}