来源与《设计模式与游戏完美开发》
UnityTool.cs
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class UnityTool
{
private List<int> a;
/// <summary>
/// 完成GameObject附加的功能,传入父 子 localPosition
/// </summary>
/// <param name="parentObj">父物体 被附着对象</param>
/// <param name="childObj">子物体 待附着</param>
/// <param name="pos">附着之后的localPosition</param>
public static void Attach(GameObject parentObj, GameObject childObj, Vector3 pos)
{
childObj.transform.parent = parentObj.transform;
childObj.transform.localPosition = pos;
}
/// <summary>
/// 完成GameObject附着在父物体的参考点上的功能(无参考点则Attach)
/// </summary>
/// <param name="parentObj">父物体</param>
/// <param name="childObj">子物体</param>
/// <param name="refPointName">被附着点的名字 </param>
/// <param name="pos">附着之后的localPosition</param>
public static void AttachToRefPos(GameObject parentObj, GameObject childObj, string refPointName, Vector3 pos)
{
//Search
bool isFinded = false;
Transform[] child = parentObj.transform.GetComponentsInChildren<Transform>();
Transform refTransform = null;
for (int i = 0; i < child.Length; i++)
{
if (child[i].name == refPointName)//找到参考点
{
i