Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/264.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/unity3d/4.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
C# 如何在unity中向预置添加纹理_C#_Unity3d_Textures - Fatal编程技术网

C# 如何在unity中向预置添加纹理

C# 如何在unity中向预置添加纹理,c#,unity3d,textures,C#,Unity3d,Textures,我有unity脚本,它将在运行时实例化对象。我需要添加一个纹理到我的预制。我经历了一些解决方案,但找不到合适的解决方案。我的代码是 using UnityEngine; using System.Collections; #if UNITY_EDITOR using UnityEditor; #endif public class createWalls : MonoBehaviour { bool creating; public GameObject start;

我有unity脚本,它将在运行时实例化对象。我需要添加一个纹理到我的预制。我经历了一些解决方案,但找不到合适的解决方案。我的代码是

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]);
                }
        }
使用UnityEngine;
使用系统集合;
#如果统一编辑器
使用UnityEditor;
#恩迪夫
公共类createWalls:单行为{
布尔创造;
公共游戏对象启动;
公共游戏对象端;
int k=0;
整数计数=0;
公共文本资产文本文件;
公共游戏对象Wallphrhab;
游戏物体壁;
公共对撞机;
公共摄像机c1;
公共摄像机c2;
公共经理;;
/*
*即使我们在建模时得到x,y值,也会将y值转换为z
*/
Vector3[]坐标=null;
向量3[]坐标y=null;
公共纹理;
/*
*用于初始化
*/
无效开始(){
readTextFileLines();
Vector3开始=新Vector3();
Vector3 end=新Vector3();
for(int i=0;i