简体   繁体   English

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

[英]Add Image component to new gameobject

I created a new game object: 我创建了一个新的游戏对象:

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

I'm looking for a way to add an image (script) for color purposes. 我正在寻找一种添加图像(脚本)以达到彩色目的的方法。 How can I do it? 我该怎么做?

This adds the Image component to your GameObject: 这会将Image组件添加到您的GameObject中:

newObject.AddComponent<Image>();

More things has to be done in order for the Image component to show: 为了使Image组件显示,还需要做更多的事情:

1 .Create a Canvas. 1。创建一个画布。 The includes creating the GameObject that will hold the Canvas then attach Canvas component to it. 其中包括创建将容纳Canvas的GameObject,然后将Canvas组件附加到其上。 You also have to attach other important UI components such as CanvasScaler and GraphicRaycaster to the Canvas . 您还必须将其他重要的UI组件(例如CanvasScalerGraphicRaycasterCanvas

2 .Create your Image GameObject with your new GameObject("ObjectName"); 2。使用new GameObject("ObjectName");创建Image GameObject new GameObject("ObjectName"); then call newObject.AddComponent<Image>(); 然后调用newObject.AddComponent<Image>(); to attach Image component to that GameObject. 将Image组件附加到该GameObject。

3 .Make that Image GameObject a child of the Canvas . 3。Image GameObject设为Canvas的子代。

This is the whole process of creating a Canvas and an Image as the child: 这是创建Canvas和Image作为子项的整个过程:

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;
}

The same way you are adding RectTransform. 添加RectTransform的方式相同。

newObject.AddComponent<Image>();

You also need to add using UnityEngine.UI to be able to find the Image component. 您还需要using UnityEngine.UI进行添加,以便能够找到Image组件。

RectTransform is mostly used for UI purposes. RectTransform主要用于UI。 If you have a canvas already set up, then you can add an Image component, but you'll want to set it as a child of your canvas: 如果已经设置了画布,则可以添加一个Image组件,但是您希望将其设置为画布的子代:

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);
    }
};

If you just want to put the sprite in the scene, you can use a SpriteRenderer : 如果只想将精灵放置在场景中,则可以使用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;
    }
}

Or to simply show a texture, you can override the main texture on the material of a MeshRenderer : 或者仅显示纹理,您可以在MeshRenderer材质上覆盖主要纹理:

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;
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 将接口实现作为组件添加到游戏对象 - Add interface implementation as a component to a gameobject 无法将脚本作为组件添加到 GameObject - Cannot add script to a GameObject as a component 在运行时将一个游戏对象组件添加到另一个具有值的游戏对象中 - add one gameobject component into another gameobject with values at runtime 如何将自定义类作为组件统一添加到游戏对象? - How to add custom class as a component to gameobject in unity? 无法向游戏对象添加任何组件 - Can't add any component to gameobject 在另一个组件上定义自定义属性时,如何将组件添加到游戏对象? - How to add a component to a gameobject when custom attribute is defined on another component? Unity C# 将材质组件添加到新游戏对象 - Unity C# Adding Material Component to new GameObject 如何在游戏对象中添加带有参数的组件,以便可以访问gameobject.newcomponent.myparameter? - How to add a component with parameters to a gameobject so that I could access it gameobject.newcomponent.myparameter? 如何动态创建一个新的游戏对象并在场景Unity3d中添加游戏对象 - How to dynamically create a new gameobject and add the gameobject in scene Unity3d 如何创建脚本列表并将其作为组件添加到新的游戏对象上 - How to create a list of scripts and add these as components onto a new gameobject
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM