1.首先, Resources.Load。不知道是u3d的设计者智障,还是我个人悟性不够。我的最终做法是中Assets目录创建了Resources文件夹。开始不知道 Resources.Load要从这个本不存在的路径加载,各种尝试,各种加载不到。
2.创建好文件夹后,放入sprite.
using UnityEngine;
using System.Collections;
using UnityEngine.UI;
using UnityEngine.Events;
using UnityEngine.SceneManagement;
public class create_sprite_by_script : MonoBehaviour {
string TAG = "CREATE_SPRITE_BY_SCRIPT";
//public Sprite[] images;
//public Ima
private Texture2D t2d;
//public Texture2D t2d;
//Sprite ui;
void createSprit(float x , float y){
getTextureById ();
//Debug.Log (TAG + "000000000 create start " );
GameObject gameObject = new GameObject();
//GameObject gameObject;
//gameObject = Instantiate (gameObject);
gameObject.AddComponent<SpriteRenderer> ();
SpriteRenderer spr = gameObject.GetComponent<SpriteRenderer>();
//Texture2D texture2d = (Texture2D)Resources.Load("hubo");
//Rect rect= new Rect();
//Rect rect = ui.textureRect;
Rect rect= new Rect();
rect.x = 0f;
rect.y = 0f;
rect.width =1920;
rect.height =1080;
//Sprite sp = Sprite.Create(t2d,spr.sprite.textureRect,new Vector2(0.5f,0.5f));
Sprite sp = Sprite.Create(t2d,rect,new Vector2(0f,0f));
//Debug.Log ("Test Screen info " + Screen.width + "---" + Screen.height );
spr.sprite = sp;
gameObject.transform.position = new Vector3(x, y,1f);
//Debug.Log (TAG + "000000000 create end " );
}
void createSprit2(){
Debug.Log (TAG + "000000000 create start " );
GameObject gameObject = new GameObject();
//GameObject gameObject;
//gameObject = Instantiate (gameObject);
gameObject.AddComponent<SpriteRenderer> ();
SpriteRenderer spr = gameObject.GetComponent<SpriteRenderer>();
//Texture2D texture2d = (Texture2D)Resources.Load("hubo");
//Rect rect= new Rect();
//Rect rect = ui.textureRect;
Rect rect= new Rect();
rect.x = 0f;
rect.y = 0f;
rect.width =Screen.width;
rect.height =Screen.height;
spr.name = "hubo";
//Sprite sp = Sprite.Create(spr.name,rect,new Vector2(0f,0f));
//Sprite sp = Sprite.Create(t2d,spr.sprite.textureRect,new Vector2(0.5f,0.5f));
//Sprite sp = Sprite.Create(t2d,rect,new Vector2(0f,0f));
spr.sortingOrder=2;
Debug.Log ("Test Screen info " + Screen.width + "---" + Screen.height );
//spr.sprite = sp;
gameObject.transform.position = new Vector3(-9.6f,-5.4f,0f);
//gameObject.AddComponent<UIWidget> ();
//spr.sortingOrder
Debug.Log (TAG + "000000000 create end " );
}
void getTextureById(){
//t2d = Resources.Load ("Assets/pic/backImage/hubo");
t2d =(Texture2D) Resources.Load ("hubo") as Texture2D;
//t2d = (Texture2D)Resources.Load ("Assets/hubo");
}
// Use this for initialization
void Start () {
//createSprit2 ();
//getTextureById();
float x;
x = -9.6f;
createSprit (x, -5.4f);
x+= 19.2f;
createSprit (x, -5.4f);
x += 19.2f;
createSprit (x, -5.4f);
}
// Update is called once per frame
void Update () {
//createSprit2 ();
}
}