[英]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组件(例如
CanvasScaler
和GraphicRaycaster
到Canvas
。
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.