Unity Game FrameWork—Component Extension—UI Component Extension

The UI component is one of the most commonly used components of UGF. The UI component and the interface group Group are created in the UIComponent interface component.

3DUI function extension

When playing VR games, the 3D UI may exist in a fixed position in the scene, or follow an object. At this time, the coordinates of the UI are no longer Vector3.zero, and the hierarchical relationship is not located in the UI of the frame component.
From the UI hierarchy, you can see that the Group group is under UI Form Instances. At this time, you can change the parent object and coordinates of the Group to achieve the desired function.
Create a UI3DComponent component, inherit from UIComponent and
create the following dictionary, the corresponding relationship between the Group name and the game object, initialize
private Dictionary<string, Transform> CustomUIGroupHelpers in awake;
rewrite the Addgroup(string uiGroupName, int depth) method, and assign
CustomUIGroupHelpers to the dictionary. Add(uiGroupName, transform);
Then create the ChangeGroupPos(string uiGroupName, Transform parents) method. After the scene switching is completed, you can call ChangeGroupPos to change the parent object parents of the Group group to complete the 3DUI function. If necessary, pass in the parents In addition, the coordinates of the Group can also be passed in. If the interface is floating, we can add follow-up logic to parents.
When it is necessary to switch scenes, call ReGroupPos to restore the Group to the frame to prevent loss.

using GameFramework;
using System.Collections.Generic;
using UnityEngine;
namespace UnityGameFramework.Runtime
{
    
    
    /// <summary>
    /// 界面组件。
    /// </summary>
    [DisallowMultipleComponent]
    [AddComponentMenu("Game Framework/UI3D")]
    public class UI3DComponent : UIComponent
    {
    
    
        private Dictionary<string, Transform> CustomUIGroupHelpers;
        private float Scale = 0.001f;
        /// <summary>
        /// 游戏框架组件初始化。
        /// </summary>
        protected override void Awake()
        {
    
    
            base.Awake();
            CustomUIGroupHelpers = new Dictionary<string, Transform>();
        }
        /// <summary>
        /// 增加界面组。
        /// </summary>
        /// <param name="uiGroupName">界面组名称。</param>
        /// <param name="depth">界面组深度。</param>
        /// <returns>是否增加界面组成功。</returns>
        public override bool AddUIGroup(string uiGroupName, int depth)
        {
    
    
            if (m_UIManager.HasUIGroup(uiGroupName))
            {
    
    
                return false;
            }
            UIGroupHelperBase uiGroupHelper = Helper.CreateHelper(m_UIGroupHelperTypeName, m_CustomUIGroupHelper, UIGroupCount);
            if (uiGroupHelper == null)
            {
    
    
                Log.Error("Can not create UI group helper.");
                return false;
            }
            uiGroupHelper.name = Utility.Text.Format("UI Group - {0}", uiGroupName);
            uiGroupHelper.gameObject.layer = LayerMask.NameToLayer("UI");
            Transform transform = uiGroupHelper.transform;
            transform.SetParent(m_InstanceRoot);
            transform.localPosition = Vector3.zero;
            transform.localEulerAngles = Vector3.zero;
            transform.localScale = Vector3.one * Scale;
            CustomUIGroupHelpers.Add(uiGroupName, transform);
            return m_UIManager.AddUIGroup(uiGroupName, depth, uiGroupHelper);
        }
        /// <summary>
        /// 改变界面组坐标
        /// </summary>
        /// <param name="uiGroupName">界面组名称。</param>
        /// <param name="depth">界面组父物体。</param>
        public bool ChangeGroupPos(string uiGroupName, Transform parents)
        {
    
    
            if (!m_UIManager.HasUIGroup(uiGroupName))
            {
    
    
                return false;
            }
            Transform transform = CustomUIGroupHelpers[uiGroupName];
            if (transform == null)
            {
    
    
                Log.Error("Can not find UI group helper.");
                return false;
            }
            transform.SetParent(parents);
            transform.localPosition = Vector3.zero;
            transform.localEulerAngles = Vector3.zero;
            transform.localScale = Vector3.one;
            return true;
        }
        /// <summary>
        /// 恢复界面组层级
        /// </summary>
        /// <param name="uiGroupName">界面组名称。</param>
        /// <param name="depth">界面组父物体。</param>
        public bool ReGroupPos(string uiGroupName)
        {
    
    
            if (!m_UIManager.HasUIGroup(uiGroupName))
            {
    
    
                return false;
            }
            Transform transform = CustomUIGroupHelpers[uiGroupName];
            if (transform == null)
            {
    
    
                Log.Error("Can not find UI group helper.");
                return false;
            }
            transform.SetParent(m_InstanceRoot);
            transform.localPosition = Vector3.zero;
            transform.localEulerAngles = Vector3.zero;
            transform.localScale = Vector3.one;
            return true;
        }
    }
}

After the code is completed, the original UI component is replaced, and the registration logic in the code is also changed to UI3D.
GameEntry. Builtin

  /// <summary>
    /// 获取界面组件。
    /// </summary>
    public static UI3DComponent UI3D
    {
    
    
        get;
        private set;
    }

insert image description here

After creating the above script and calling it, you will find that the initial coordinates do not match. Then open the script DefaultUIFormHelper, and reset the local coordinates in the CreateUIForm method
transform.localPosition = Vector3.zero;
transform.localEulerAngles = Vector3.zero;
Note: the UI under the 3Dgroup needs to be set to World Space. Generally, you can use only one 3DUI and 2DUI. If you use them together, you have not tried them. Interested friends can explore them.

Guess you like

Origin blog.csdn.net/qq_37619255/article/details/129186961