Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/user-interface/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Image UnityUI以编程方式添加图像_Image_User Interface_Unity3d - Fatal编程技术网

Image UnityUI以编程方式添加图像

Image UnityUI以编程方式添加图像,image,user-interface,unity3d,Image,User Interface,Unity3d,我使用以下代码将图像添加到画布 但是,图像位于左下角 我希望图像位于画布的中心 任何人都可以帮助我 using System.Collections; using System.Collections.Generic; using UnityEngine; using UnityEngine.UI; public class TestController : MonoBehaviour { public GameObject canvas; //public Sprite r

我使用以下代码将图像添加到画布 但是,图像位于左下角 我希望图像位于画布的中心 任何人都可以帮助我

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

public class TestController : MonoBehaviour {

    public GameObject canvas;
    //public Sprite refSprite;

    // Use this for initialization
    void Start () {

        GameObject imgObject = new GameObject("testAAA");

        RectTransform trans = imgObject.AddComponent<RectTransform>();
        trans.anchoredPosition = new Vector2(0.5f, 0.5f);
        trans.localPosition = new Vector3(0, 0, 0);
        trans.position = new Vector3(0, 0, 0);


        Image image = imgObject.AddComponent<Image>();
        Texture2D tex = Resources.Load<Texture2D>("red");
        image.sprite = Sprite.Create(tex, new Rect(0, 0, tex.width, tex.height), new Vector2(0.5f, 0.5f));
        imgObject.transform.SetParent(canvas.transform);


    }

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

    }
}
使用系统集合;
使用System.Collections.Generic;
使用UnityEngine;
使用UnityEngine.UI;
公共类TestController:MonoBehavior{
公共游戏对象画布;
//公共雪碧;
//用于初始化
无效开始(){
GameObject imgObject=新游戏对象(“testAAA”);
RectTransform trans=imgObject.AddComponent();
跨锚定位置=新向量2(0.5f,0.5f);
trans.localPosition=新向量3(0,0,0);
trans.position=新矢量3(0,0,0);
Image=imgObject.AddComponent();
Texture2D tex=Resources.Load(“红色”);
image.sprite=sprite.Create(tex,new Rect(0,0,tex.width,tex.height),new Vector2(0.5f,0.5f));
imgObject.transform.SetParent(canvas.transform);
}
//每帧调用一次更新
无效更新(){
}
}

必须先设置父级,然后再设置位置/旋转/缩放

void Start () {
    GameObject imgObject = new GameObject("testAAA");

    RectTransform trans = imgObject.AddComponent<RectTransform>();
    trans.transform.SetParent(canvas.transform); // setting parent
    trans.localScale = Vector3.one;
    trans.anchoredPosition = new Vector2(0f, 0f); // setting position, will be on center
    trans.sizeDelta= new Vector2(150, 200); // custom size

    Image image = imgObject.AddComponent<Image>();
    Texture2D tex = Resources.Load<Texture2D>("red");
    image.sprite = Sprite.Create(tex, new Rect(0, 0, tex.width, tex.height), new Vector2(0.5f, 0.5f));
    imgObject.transform.SetParent(canvas.transform);
}
void Start(){
GameObject imgObject=新游戏对象(“testAAA”);
RectTransform trans=imgObject.AddComponent();
trans.transform.SetParent(canvas.transform);//设置父对象
trans.localScale=Vector3.1;
trans.anchoredPosition=新矢量2(0f,0f);//设置位置,将位于中心
trans.sizeDelta=新向量2(150200);//自定义大小
Image=imgObject.AddComponent();
Texture2D tex=Resources.Load(“红色”);
image.sprite=sprite.Create(tex,new Rect(0,0,tex.width,tex.height),new Vector2(0.5f,0.5f));
imgObject.transform.SetParent(canvas.transform);
}