我有统一脚本,它将在运行时实例化对象。我需要在预制件上添加纹理。我经历了一些解决方案,但我找不到合适的解决方案。我的代码是
using UnityEngine;
using System.Collections;
#if UNITY_EDITOR
using UnityEditor;
#endif
public class createWalls : MonoBehaviour {
bool creating;
public GameObject start;
public GameObject end;
int k=0;
int count = 0;
public TextAsset TextFile;
public GameObject wallPrehab;
GameObject wall;
public Collider coll;
public Camera c1;
public Camera c2;
public Manager mgr;
/*
*even we though it get as x,y values when modeling convert it's y value as z
*/
Vector3[] coordinatesX = null;
Vector3[] coordinatesY = null;
public Texture texture;
/*
* Use this for initialization
*/
void Start () {
readTextFileLines();
Vector3 start = new Vector3();
Vector3 end = new Vector3();
for (int i = 0; i < coordinatesX.Length; i++)
{
k = i + 1;
if (k != coordinatesX.Length)
{
if(coordinatesX[i].x == coordinatesX[k].x){
start = coordinatesX[i];
end = coordinatesX[k];
setStart(start);
setEnd(end);
adjust();
}
}
}
for(int j = 0; j < coordinatesY.Length; j++){
k = j + 1;
if (k != coordinatesY.Length)
{
if(coordinatesY[j].z == coordinatesY[k].z){
start = coordinatesY[j];
end = coordinatesY[k];
setStart(start);
setEnd(end);
adjust();
}
}
}
}
// Update is called once per frame
void Update () {
//getinput();
}
/*
* getting the mouse clicked position coordinate
*/
int number=0;
void setStart(Vector3 x){
creating = true;
start.transform.position = x;
wall = Instantiate (wallPrehab, start.transform.position, Quaternion.identity)as GameObject;
wall.GetComponent<WallScript>().mgr=mgr;
wall.gameObject.name = number.ToString ();
number++;
}
/*
* getting the mouse click over position coordinate
*/
void setEnd(Vector3 y){
end.transform.position = y;
}
/*
* invoking the wall building method
*/
void adjust(){
adjustWall ();
}
/*
* build the wall in between start point and the end point
*/
void adjustWall(){
start.transform.LookAt (end.transform.position);
end.transform.LookAt (start.transform.position);
float distance = Vector3.Distance (start.transform.position, end.transform.position);
wall.transform.position = start.transform.position + distance / 2 * start.transform.forward;
wall.transform.rotation = start.transform.rotation;
wall.transform.localScale = new Vector3 (wall.transform.localScale.x, wall.transform.localScale.y, distance);
}
/*
* Reading from the text file
*/
void readTextFileLines()
{
int count = 0;
string splits = TextFile.text.TrimStart ('[');
string[] split = TextFile.text.Split (')');
string split_1 = null;
string split_2 = null;
string split_3 = null;
int pos = 0;
int lengthOfString = 0;
int valX, valZ, valX1, valZ1 = 0;
/*
* Getting the count of the coordinates in the array
*/
foreach (string x in split) {
count++;
}
string[] stringArr = new string[count];
int[] xSortX = new int[count];
int[] xSortZ = new int[count];
int[] ySortX = new int[count];
int[] ySortZ = new int[count];
/*
* Splitting the coordinates as x,y and store in an array
*/
foreach (string coord in split) {
split_1 = coord;
split_1 = split_1.Trim ('[');
split_1 = split_1.Trim ('(');
split_1 = split_1.Trim (',');
split_1 = split_1.Trim (' ');
split_1 = split_1.Trim ('(');
split_1 = split_1.TrimEnd (']');
stringArr [pos] = split_1;
pos++;
}
//Debug.Log("Array Length " + stringArr.Length);
/*
* extracting simalar x coordinates
*/
//Debug.Log("");
//Debug.Log("-----------------extracting simalar x coordinates----------------");
//Debug.Log("");
int indexX = 0;
int loopRunX = 0;
for (int a = 0; a < stringArr.Length - 1; a = indexX) {
split_2 = stringArr [a];
lengthOfString = split_2.Length;
valX = int.Parse (split_2.Substring (0, split_2.IndexOf (',')));
valZ = int.Parse (split_2.Substring (split_2.IndexOf (' '), (lengthOfString - split_2.IndexOf (' '))));
for (int x1 = indexX; x1 < stringArr.Length - 1; x1++) {
split_3 = stringArr [x1];
lengthOfString = split_3.Length;
valX1 = int.Parse (split_3.Substring (0, split_3.IndexOf (',')));
valZ1 = int.Parse (split_3.Substring (split_3.IndexOf (' '), (lengthOfString - split_3.IndexOf (' '))));
//Check for the simillar x in the text file we provide
if (valX == valX1) {
xSortX [indexX] = valX1;
xSortZ [indexX] = valZ1;
//Debug.Log("X is " + valX + " and the coordinates which have simillar x ==> (" + valX1 + ", " + valZ1 + "). Index is " + x1);
//Debug.Log("xSortX["+indexX+"] is :"+xSortX[indexX]);
//Debug.Log("xSortZ["+indexX+"] is :"+xSortZ[indexX]);
indexX++;
} else {
break;
}
}
loopRunX++;
//indexX = indexX + countx;
//Debug.Log("Next Index to check onwards : " + indexX);
//Debug.Log("Looping Count : " + loopRunX);
//Debug.Log("");
}
coordinatesX = new Vector3[count];
/*
* Adding the x and z coorinates values to Vector3 array to build the object in coordinates x
*/
for (int x =0; x<count; x++) {
Vector3 createVArray = new Vector3 (xSortX [x], 0, xSortZ [x]);
coordinatesX [x] = createVArray;
//Debug.Log("Coordinates X :"+coordinatesX[x]);
}
/*
* extracting simalar x coordinates
*/
//Debug.Log("");
//Debug.Log("-----------------extracting simalar Y coordinates----------------");
//Debug.Log("");
int[] checker = new int[count];
bool eq = false;
int indexY = 0;
loopRunX = 0;
for (int a = 0; a < stringArr.Length - 1; a++) {
split_2 = stringArr [a];
lengthOfString = split_2.Length;
valX = int.Parse (split_2.Substring (0, split_2.IndexOf (',')));
valZ = int.Parse (split_2.Substring (split_2.IndexOf (' '), (lengthOfString - split_2.IndexOf (' '))));
foreach (int check in checker) {
if (check == valZ) {
eq = false;
break;
} else {
eq = true;
}
}
if (eq) {
checker [a] = valZ;
for (int x1 = a; x1 < stringArr.Length - 1; x1++) {
split_3 = stringArr [x1];
lengthOfString = split_3.Length;
valX1 = int.Parse (split_3.Substring (0, split_3.IndexOf (',')));
valZ1 = int.Parse (split_3.Substring (split_3.IndexOf (' '), (lengthOfString - split_3.IndexOf (' '))));
//Check for the simillar x in the text file we provide
if (valZ == valZ1) {
ySortX [indexY] = valX1;
ySortZ [indexY] = valZ1;
//Debug.Log("Y is " + valZ + " and the coordinates which have simillar z ==> (" + valX1 + ", " + valZ1 + "). Index is " + x1);
//Debug.Log("ySortX["+indexY+"] is :"+ySortX[indexY]);
//Debug.Log("ySortZ["+indexY+"] is :"+ySortZ[indexY]);
indexY++;
}
}
loopRunX++;
}
//Debug.Log("");
}
coordinatesY = new Vector3[count];
/*
* Adding the x and z coorinates values to Vector3 array to build the object in coordinates x
*/
for (int x =0; x<count; x++) {
Vector3 createVArray = new Vector3 (ySortX [x], 0, ySortZ [x]);
coordinatesY [x] = createVArray;
//Debug.Log("Coordinates Y :"+coordinatesY[x]);
}
}
我需要将此纹理添加到我的gameObject预制件
中