Resources.load 和Instantiate 的理解
1 Resources.load 是将物体加载到内存中去,
2 Instantitate 是在Unity 中生成该物体,之后场景中就会出现该物体 示例: using UnityEngine;
using System.Collections;
using System.Collections.Generic;
public class Choose_LoadObject : MonoBehaviour { public List<Transform> transformList;
public List<string> contentList= new List<string>();
private string server;
public GameObject floatingPrefab;
private GameObject target;
// Use this for initialization
void Start () {
server = "http://www.vrbeyond.com/kejian/";
// transformList = new List<Transform>();
PlayerPrefs.SetString("server", server);
StartCoroutine(Loadobj());
target = GameObject.Find("Target");
}
// Update is called once per frame
void Update()
{ } IEnumerator Loadobj() {
string pathJD= server + "JD.php";
using (WWW contentText = new WWW(pathJD))
{
yield return contentText;
string contentString = contentText.text;
//Debug.Log(contentString);
string [] contentStringArray= contentString.Split('\n');
for(int i = 0; i < contentStringArray.Length; i++)
{
if (i == 0) { continue; }
if (i == 2)
{
break;
}
string[] contentItemStringArray= contentStringArray[i].Split('$');
GameObject go= Instantiate<GameObject>(floatingPrefab);
go.name = contentItemStringArray[0]; // 节点
go.transform.Find("nameSprite/nametext").GetComponent<TextMesh>().text = contentItemStringArray[1]; //名字
go.transform.position = transformList[i].position;
// go.transform.LookAt(target.transform); string prefabPath = "Hourse" + "/" + contentItemStringArray[0] + "/" + contentItemStringArray[0] + "_GJJG_Hand"; // 加载到内存中去,
GameObject modle= Resources.Load<GameObject>(prefabPath); // 在场景中生成物体,生成后场景就会出现该物体实例
GameObject modleGo = Instantiate<GameObject>(modle);
modleGo.transform.SetParent(go.transform);
modleGo.transform.localScale = new Vector3(0.12f, 0.12f, 0.12f);
modleGo.transform.localPosition = new Vector3(0f, 0f, 0.5825443f); modleGo.transform.Find("_标注").gameObject.SetActive(false);
modleGo.transform.Find("跟踪物体").gameObject.SetActive(false);
// 加载但是没在场景中生成,但是场景中没有该物体,之后把图片赋值给要显示的UI 上
string spritePath = "Hourse" + "/" + contentItemStringArray[0] + "/" + contentItemStringArray[0] + "_sprite";
Sprite s = Resources.Load<Sprite>(spritePath);
go.transform.Find("nameSprite").GetComponent<SpriteRenderer>().sprite = s; }
} }
}