Unity GameFramework框架使用UGUI无法显示问题
原因
这是由于UGUI的显示依赖于canvas,而框架自动生成的UI Form Instances对象的没有canvas,导致生成在下面的UI控件无法显示。
解决方案
- 在UI下新增UI Form Instances空对象;
- 配置UI Form Instances对象下的插件(增加Rect Transform, Canvas, Canvas Scaler, Graphic Raycaster)
- 将UI Form Instances拖到UI对象的Instance Root;
- 此时UI已可以正常显示,但是锚点可能还存在问题;这是因为UI对象的父级没有canvas,通过自定义UI Group Help可以解决;
- 创建自定义的help类;
using UnityEngine;
using UnityEngine.UI;
using UnityGameFramework.Runtime;
namespace Flower
{
/// <summary>
/// uGUI 界面组辅助器。
/// </summary>
public class UGuiGroupHelper : UIGroupHelperBase
{
public const int DepthFactor = 100;
private int m_Depth = 0;
private Canvas m_CachedCanvas = null;
/// <summary>
/// 设置界面组深度。
/// </summary>
/// <param name="depth">界面组深度。</param>
public override void SetDepth(int depth)
{
m_Depth = depth;
m_CachedCanvas.overrideSorting = true;
m_CachedCanvas.sortingOrder = DepthFactor * depth;
}
private void Awake()
{
m_CachedCanvas = gameObject.GetOrAddComponent<Canvas>();
gameObject.GetOrAddComponent<GraphicRaycaster>();
}
private void Start()
{
m_CachedCanvas.overrideSorting = true;
m_CachedCanvas.sortingOrder = DepthFactor * m_Depth;
RectTransform transform = GetComponent<RectTransform>();
transform.anchorMin = Vector2.zero;
transform.anchorMax = Vector2.one;
transform.anchoredPosition = Vector2.zero;
transform.sizeDelta = Vector2.zero;
}
}
}
- 修改UI对象的UI Group Help为自定义的help;
问题解决!