Gerolmed Geschrieben 21. April 2017 Melden Share Geschrieben 21. April 2017 Im trying to load a Level from a texture 2d but a GameObject is only instanciated if the Color is black Here are my two scripts: using UnityEngine; using System.Collections; public class LoadMap : MonoBehaviour { public Texture2D level1; Texture2D levelToLoad; Objects objects; // Use this for initialization void Start () { objects = this.GetComponent<Objects> (); loadLevel (0); renderLevel (); } // Update is called once per frame void Update () { } void loadLevel(int i) { switch (i) { case 0: levelToLoad = level1; break; default: break; } } void renderLevel() { int pixelNum = 0; for(int x = levelToLoad.width; x > 0; x--) { for(int y = levelToLoad.height; y > 0; y--) { GameObject objectNotCreated = objects.getGameObjectByColor (levelToLoad.GetPixel (x, y)); GameObject objcreated = null; if(objectNotCreated != null) { objcreated = GameObject.Instantiate (objectNotCreated); objcreated.transform.SetParent (this.transform); objcreated.transform.position = new Vector3 (x - levelToLoad.width,-(levelToLoad.height - y), 0); } pixelNum++; } } } } And: using System.Collections; using System.Collections.Generic; using UnityEngine; public class Objects : MonoBehaviour { public Color[] colors; public GameObject[] objects; void start() { } public GameObject getGameObjectByColor(Color color) { for (int i = 0; i < colors.Length; i++) { if(colors[i].r == color.r && colors[i].g == color.g && colors[i].b == color.b && colors[i].a == color.a) { return objects [i]; } } return null; } } Link zu diesem Kommentar Auf anderen Seiten teilen More sharing options...
Sascha Geschrieben 21. April 2017 Melden Share Geschrieben 21. April 2017 That's good to know, thanks for the heads up! On a more serious note, you're comparing the parameter color with colors[1], not colors. Link zu diesem Kommentar Auf anderen Seiten teilen More sharing options...
Gerolmed Geschrieben 21. April 2017 Autor Melden Share Geschrieben 21. April 2017 vor 54 Minuten schrieb Sascha: That's good to know, thanks for the heads up! On a more serious note, you're comparing the parameter color with colors[1], not colors. Oh! In MonoDevelope I and 1 look very similarly. It still isn't working with an other color than black in the color array. Link zu diesem Kommentar Auf anderen Seiten teilen More sharing options...
Sascha Geschrieben 21. April 2017 Melden Share Geschrieben 21. April 2017 The float values perhaps don't exactly match. In your array, you have (0.5,0,0) and your parameter would be (0.4999,0,0) or something. Put a print statement between your for loop and if clause: print(color + " vs. " + colors[i]); Link zu diesem Kommentar Auf anderen Seiten teilen More sharing options...
Recommended Posts
Archiviert
Dieses Thema ist jetzt archiviert und für weitere Antworten gesperrt.