Unity自定义UI

本文介绍如何在Unity中自定义创建UI组件,包括Text、Slider和ScrollView,通过编写脚本来快速生成并设置参数,提高开发效率。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Unity自定义UI

在我们使用Unity开发一个项目的时候,往往会频繁的创建和修改UGUI的组件或其参数。由于我们所需要的功能往往与UGUI自动创建的组件不能够完全匹配,在每次创建UGUI组件的时候都要花时间来修改相应参数,故我们可以自定义我们自己的UI来减少相应的麻烦。


using UnityEditor;
using UnityEngine;
using UnityEngine.UI;

/// <summary>
/// 自定义UI组件
/// </summary>

public class UICreate
{

    //自定义Text
    [MenuItem("UI/Text", false, 1)]
    private static void MyCreateText()
    {
        if (GameObject.FindGameObjectWithTag("UI") == null)
        {
            Debug.LogError("没有Tag为UI的Canvas");
            return;
        }
        GameObject UItext = new GameObject("MyText");
        UItext.transform.parent=GameObject.FindGameObjectWithTag("UI").transform;
        UItext.transform.localPosition = Vector3.zero;
        UItext.AddComponent<Text>();
        Text MyText = UItext.GetComponent<Text>();
        MyText.fontSize = 30;
        MyText.text = "Gay里Gay气";
        MyText.raycastTarget = false;
    }
    //自定义Slider
    [MenuItem("UI/Slider", false, 2)]
    private static void MyCreateSlider()
    {
        if (GameObject.FindGameObjectWithTag("UI") == null)
        {
            Debug.LogError("没有Tag为UI的Canvas");
            return;
        }
        GameObject UISlider = new GameObject("MySlider");

        UISlider.transform.parent = GameObject.FindGameObjectWithTag("UI").transform;
        UISlider.transform.localPosition = Vector3.zero;
       
        UISlider.AddComponent<Slider>();
        UISlider.GetComponent<RectTransform>().sizeDelta = new Vector2(500, 50);

        GameObject UISliderBg = new GameObject("Bg");
        UISliderBg.transform.parent = UISlider.transform;
        UISliderBg.transform.localPosition = Vector3.zero;
        UISliderBg.AddComponent<Image>();
        UISliderBg.GetComponent<RectTransform>().SetSizeWithCurrentAnchors(RectTransform.Axis.Horizontal, 0);
        UISliderBg.GetComponent<RectTransform>().SetSizeWithCurrentAnchors(RectTransform.Axis.Vertical, 0);

        GameObject UISliderFill = new GameObject("Fill");
        UISliderFill.transform.parent = UISliderBg.transform;
        UISliderFill.transform.localPosition = Vector3.zero;
        UISliderFill.AddComponent<Image>();
        UISliderFill.GetComponent<RectTransform>().SetSizeWithCurrentAnchors(RectTransform.Axis.Horizontal, 0);
        UISliderFill.GetComponent<RectTransform>().SetSizeWithCurrentAnchors(RectTransform.Axis.Vertical, 0);

        Slider MySlider = UISlider.GetComponent<Slider>();
        MySlider.interactable = false;
        MySlider.fillRect = UISliderBg.transform as RectTransform;

        UISliderBg.GetComponent<RectTransform>().anchorMax = new Vector2(1, 1);
        MySlider.fillRect = UISliderFill.transform as RectTransform;

    }
    //自定义Scroll View
    [MenuItem("UI/Scroll View", false, 3)]
    private static void MyCreateScrollView()
    {
        if (GameObject.FindGameObjectWithTag("UI") == null)
        {
            Debug.LogError("没有Tag为UI的Canvas");
            return;
        }
        //设置物体
        GameObject UIScrollView = new GameObject("MyScrollView");
        UIScrollView.transform.parent = GameObject.FindGameObjectWithTag("UI").transform;
        UIScrollView.transform.localPosition = Vector3.zero;
        UIScrollView.AddComponent<ScrollRect>();
        ScrollRect MyScrollView = UIScrollView.GetComponent<ScrollRect>();
        UIScrollView.GetComponent<RectTransform>().sizeDelta = new Vector2(1200, 800);


        GameObject UIViewPort = new GameObject("View");
        UIViewPort.transform.parent = UIScrollView.transform;
        UIViewPort.transform.localPosition = Vector3.zero;
        UIViewPort.AddComponent<Image>();
        UIViewPort.AddComponent<Mask>();
        UIViewPort.GetComponent<Mask>().showMaskGraphic = false;


        GameObject UIViewContent = new GameObject("Content");
        UIViewContent.transform.parent = UIViewPort.transform;
        UIViewContent.transform.localPosition = Vector3.zero;
        UIViewContent.AddComponent<RectTransform>();

        //设置位置
        MyScrollView.viewport = UIViewPort.transform as RectTransform;
        MyScrollView.content = UIViewContent.transform as RectTransform;
        UIViewPort.GetComponent<RectTransform>().SetSizeWithCurrentAnchors(RectTransform.Axis.Horizontal, 0);
        UIViewPort.GetComponent<RectTransform>().SetSizeWithCurrentAnchors(RectTransform.Axis.Vertical, 0);
        UIViewPort.GetComponent<RectTransform>().anchorMax = new Vector2(1, 1);
        UIViewPort.GetComponent<RectTransform>().anchorMin = new Vector2(0, 0);
        UIViewPort.GetComponent<RectTransform>().pivot = new Vector2(0, 1);
        UIViewContent.GetComponent<RectTransform>().SetSizeWithCurrentAnchors(RectTransform.Axis.Horizontal, 0);
        UIViewContent.GetComponent<RectTransform>().SetSizeWithCurrentAnchors(RectTransform.Axis.Vertical, 0);
        UIViewContent.GetComponent<RectTransform>().anchorMax = new Vector2(1, 1);
        UIViewContent.GetComponent<RectTransform>().anchorMin = new Vector2(0, 1);
        UIViewContent.GetComponent<RectTransform>().pivot = new Vector2(0, 1);


        //设置组件
        UIViewContent.AddComponent<GridLayoutGroup>();
        GridLayoutGroup ContentGrid = UIViewContent.GetComponent<GridLayoutGroup>();
        ContentGrid.padding.left = 20;
        ContentGrid.padding.top = 20;
        ContentGrid.cellSize = new Vector2(200, 200);
        ContentGrid.spacing = new Vector2(40, 40);
        UIViewContent.AddComponent<ContentSizeFitter>();
        ContentSizeFitter ContentFitter = UIViewContent.GetComponent<ContentSizeFitter>();
        ContentFitter.verticalFit = ContentSizeFitter.FitMode.PreferredSize;

    }
}

 

评论 5
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值