将图像组件添加到新游戏对象

Add Image component to new gameobject

我创建了一个新的游戏对象:

GameObject newObject = new GameObject("ObjectName");
newObject.AddComponent<RectTransform>();
newObject.GetComponent<RectTransform>().sizeDelta = new Vector2(width, height);

我正在寻找一种方法来添加图像(脚本)以用于颜色目的。 我该怎么做?

与添加 RectTransform 的方式相同。

newObject.AddComponent<Image>();

您还需要添加 using UnityEngine.UI 才能找到图像组件。

这会将图像组件添加到您的游戏对象中:

newObject.AddComponent<Image>();

要显示 Image 组件还需要做更多的事情:

1。创建一个Canvas。这包括创建将容纳 Canvas 然后将 Canvas 组件附加到它的游戏对象。您还必须将其他重要的 UI 组件(例如 CanvasScalerGraphicRaycaster 附加到 Canvas.

2。使用 new GameObject("ObjectName"); 创建 Image 游戏对象,然后调用 newObject.AddComponent<Image>(); 将图像组件附加到该游戏对象。

3。使 Image GameObject 成为 Canvas.

的 child

这是创建一个Canvas和一个图像作为child的整个过程:

void Start()
{
    //Create Canvas
    GameObject canvas = createCanvas(false);

    //Create your Image GameObject
    GameObject newObject = new GameObject("ObjectName");

    //Make the GameObject child of the Canvas
    newObject.transform.SetParent(canvas.transform);

    //Add Image Component to it(This will add RectTransform as-well)
    newObject.AddComponent<Image>();

    //Center Image to screen
    newObject.GetComponent<RectTransform>().anchoredPosition = Vector2.zero;
}



//Creates Hidden GameObject and attaches Canvas component to it
private GameObject createCanvas(bool hide)
{
    //Create Canvas GameObject
    GameObject tempCanvas = new GameObject("Canvas");
    if (hide)
    {
        tempCanvas.hideFlags = HideFlags.HideAndDontSave;
    }

    //Create and Add Canvas Component
    Canvas cnvs = tempCanvas.AddComponent<Canvas>();
    cnvs.renderMode = RenderMode.ScreenSpaceOverlay;
    cnvs.pixelPerfect = false;

    //Set Cavas sorting order to be above other Canvas sorting order
    cnvs.sortingOrder = 12;

    cnvs.targetDisplay = 0;

    addCanvasScaler(tempCanvas);
    addGraphicsRaycaster(tempCanvas);
    return tempCanvas;
}

//Adds CanvasScaler component to the Canvas GameObject 
private void addCanvasScaler(GameObject parentaCanvas)
{
    CanvasScaler cvsl = parentaCanvas.AddComponent<CanvasScaler>();
    cvsl.uiScaleMode = CanvasScaler.ScaleMode.ScaleWithScreenSize;
    cvsl.referenceResolution = new Vector2(800f, 600f);
    cvsl.matchWidthOrHeight = 0.5f;
    cvsl.screenMatchMode = CanvasScaler.ScreenMatchMode.MatchWidthOrHeight;
    cvsl.referencePixelsPerUnit = 100f;
}

//Adds GraphicRaycaster component to the Canvas GameObject 
private void addGraphicsRaycaster(GameObject parentaCanvas)
{
    GraphicRaycaster grcter = parentaCanvas.AddComponent<GraphicRaycaster>();
    grcter.ignoreReversedGraphics = true;
    grcter.blockingObjects = GraphicRaycaster.BlockingObjects.None;
}

RectTransform 主要用于 UI 目的。如果您已经设置了 canvas,那么您可以添加一个 Image 组件,但您需要将其设置为 canvas:

的子组件
using UnityEngine;
using UnityEngine.UI;

public class ImageTest : MonoBehaviour {
    public Canvas canvas;
    public Sprite sprite;
    public float width = 10;
    public float height = 10;

    private void Start () {
        GameObject newObject = new GameObject("ObjectName");
        RectTransform rectTransform = newObject.AddComponent<RectTransform>();
        rectTransform.sizeDelta = new Vector2(width, height);
        Image image = newObject.AddComponent<Image>();
        image.sprite = sprite;
        newObject.transform.SetParent(canvas.transform, false);
    }
};

如果你只想把精灵放在场景中,你可以使用SpriteRenderer:

using UnityEngine;

public class SpriteTest : MonoBehaviour {
    public Sprite sprite;

    private void Start () {
        GameObject newObject = new GameObject("ObjectName");
        SpriteRenderer spriteRenderer = newObject.AddComponent<SpriteRenderer>();
        spriteRenderer.sprite = sprite;
    }
}

或者为了简单地显示纹理,您可以覆盖 MeshRenderer 的 material 上的主纹理:

using UnityEngine;

public class TextureTest : MonoBehaviour {
    public Texture texture;

    private void Start () {
        GameObject newObject = GameObject.CreatePrimitive(PrimitiveType.Quad);
        MeshRenderer meshRenderer = newObject.GetComponent<MeshRenderer>();
        meshRenderer.material.mainTexture = texture;
    }
}