Ejemplo n.º 1
0
    /// <summary>
    /// 挂载武器
    /// </summary>
    /// <param name="root">主角对象</param>
    /// <param name="attachPoint">挂载点</param>
    /// <param name="weapon">武器对象</param>
    /// <returns></returns>
    public static bool AttachWeapon(GameObject root, AttachPoint attachPoint, GameObject weapon, GameObject WeaponEff)
    {
        bool      flag       = false;
        Transform attachment = null;
        string    parentName = attachPoint.GetAttachment();

        if (!string.IsNullOrEmpty(parentName))
        {
            flag = root.transform.RecursiveFindObject(parentName, out attachment);
            if (flag)
            {
                var m_child = UnityEngine.Object.Instantiate(weapon) as GameObject;
                m_child.name = weapon.name;

                attachment.ClearChild();

                // Parent the child to this object

                Transform ct = m_child.transform;
                ct.parent = attachment;
                // Reset the pos/rot/scale, just in case
                ct.localPosition = Vector3.zero;
                ct.localRotation = Quaternion.identity;
                ct.localScale    = Vector3.one;
                AttachWeaponEff(m_child, WeaponEff);
            }
        }

        return(flag);
    }