Unity使用UGUI的text创建任意文字对cube贴图

版权声明:本文为博主原创文章,如需转载,请注明出处: https://blog.csdn.net/MASILEJFOAISEGJIAE/article/details/83552517

假设我们要给一个正方体的六个面都标记上任意,例如数字,效果如下:
在这里插入图片描述
则可以使用UGUI的text,以及RawImage作为贴图。

新建一个GameObject,Add Component 以下脚本即可:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

using UnityEngine.UI;

public class CubeCotroller : MonoBehaviour
{

    // Use this for initialization
    void Start()
    {
        GameObject cube = scriptCube.createCube("Cube", 40);
    }


    private GameObject target;

    // Update is called once per frame
    void Update()
    {
    }
}

public class scriptCube
{
    static GameObject addSide(int size, string text)
    {
        // First we create a canvas-object to hold the UI:

        GameObject mainObj = new GameObject();
        Canvas canvasObj = mainObj.AddComponent<Canvas>();
        canvasObj.renderMode = RenderMode.WorldSpace;
        // Then we create a rawimage-object and we connect it to the parent object:

        GameObject childObj2 = new GameObject();
        RawImage rawimageObj = childObj2.AddComponent<RawImage>();
        rawimageObj.rectTransform.SetSizeWithCurrentAnchors
             (RectTransform.Axis.Horizontal, size);
        rawimageObj.rectTransform.SetSizeWithCurrentAnchors
             (RectTransform.Axis.Vertical, size);
        rawimageObj.color = Color.yellow;
        childObj2.transform.SetParent(mainObj.transform, false);

        // We also have to create the text-object and connect it to the parent object:
        GameObject childObj1 = new GameObject();
        Text textObj = childObj1.AddComponent<Text>();
        textObj.font = (Font)Resources.GetBuiltinResource
            (typeof(Font), "Arial.ttf"); ;
        textObj.text = text;
        textObj.alignment = TextAnchor.MiddleCenter;
        textObj.enabled = true;
        textObj.fontSize = (int)(size * 0.8);
        textObj.color = Color.black;
        textObj.rectTransform.SetSizeWithCurrentAnchors
            (RectTransform.Axis.Horizontal, size);
        textObj.rectTransform.SetSizeWithCurrentAnchors
            (RectTransform.Axis.Vertical, size);
        childObj1.transform.SetParent(mainObj.transform, false);

        return mainObj;
    }

    public static GameObject createCube(string name, int size)
    {

        GameObject mainObj = new GameObject();
        mainObj.name = name;

        GameObject side1 = addSide(size, "1");
        side1.transform.SetParent(mainObj.transform);
        side1.transform.position = new Vector3(0, 0, -size / 2);
        side1.transform.rotation = Quaternion.Euler(0, 0, 0);

        GameObject side2 = addSide(size, "6");
        side2.transform.SetParent(mainObj.transform);
        side2.transform.position = new Vector3(0, 0, size / 2);
        side2.transform.rotation = Quaternion.Euler(0, 180, 0);

        GameObject side3 = addSide(size, "3");
        side3.transform.SetParent(mainObj.transform);
        side3.transform.position = new Vector3(0, -size / 2, 0);
        side3.transform.rotation = Quaternion.Euler(90, 0, 0);

        GameObject side4 = addSide(size, "4");
        side4.transform.SetParent(mainObj.transform);
        side4.transform.position = new Vector3(0, size / 2, 0);
        side4.transform.rotation = Quaternion.Euler(90, 0, 0);

        GameObject side5 = addSide(size, "2");
        side5.transform.SetParent(mainObj.transform);
        side5.transform.position = new Vector3(-size / 2, 0, 0);
        side5.transform.rotation = Quaternion.Euler(0, 90, 0);

        GameObject side6 = addSide(size, "5");
        side6.transform.SetParent(mainObj.transform);
        side6.transform.position = new Vector3(size / 2, 0, 0);
        side6.transform.rotation = Quaternion.Euler(0, 270, 0);

        mainObj.transform.rotation = Quaternion.Euler(45, 45, 45);

        return mainObj;
    }
}

参考资料:http://geek1337.blogspot.com/2017/04/unity3d-creating-cube-with-text-on-each.html

猜你喜欢

转载自blog.csdn.net/MASILEJFOAISEGJIAE/article/details/83552517