Jump to content
Unity Insider Forum

Loading Levels from Texture2D color compare error


Gerolmed

Recommended Posts

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

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

Archiviert

Dieses Thema ist jetzt archiviert und für weitere Antworten gesperrt.

×
×
  • Neu erstellen...