Наследование: UIBaseObject
		}//Start()


		public void Load(string name, UIVideo video)
		{
			//we only create new video for the first load!
			if(((!loadedFlag)&&(name!=myname))||(name!=myname))
			{
				loadedFlag = true;
				myname = name;
				StartCoroutine(LoadExternalCoroutine(name, video));
			}
		}
		IEnumerator LoadExternalCoroutine(string name, UIVideo video)
		{
			//load only once
			string url = "file://" + GameManager.Instance.mainGame.ExternalResourcePath + "/" + name + ".ogv";
			// Debug.Log(">>>>>>> Load file:" + url);
			//WebPlayerDebugManager.addOutput("_______ LoadExternalCoroutine url: "+url, 1);

			//release resources for www
			DestroyImmediate(video.movie);
			DestroyImmediate(video.GameObject.GetComponent<Renderer>().material.mainTexture);
			video.movie = null;

			WWW www = new WWW(url);

			// while (!www.movie.isReadyToPlay);

			yield return www;
			
			if (string.IsNullOrEmpty(www.error))
			{
				video.movie = www.movie;
				video.GameObject.GetComponent<Renderer>().material.mainTexture = video.movie;
				video.GameObject.GetComponent<Renderer>().material.color = Color.white;
				video.movie.Play(); //hack: get the first frame
				video.movie.Stop();
			}
			else
			{
				Debug.LogWarning(www.error);
				MovieTexture t = Resources.Load<MovieTexture>(name);
				if (t != null)
				{
					video.movie = t;
					video.GameObject.GetComponent<Renderer>().material.mainTexture = video.movie;
				}
			}

			www.Dispose();
			www = null;
		}
		}//LoadResources()

		/// <summary>
		/// Load object resources from dictionary
		/// TODO make more generic helpers and way of parse similar stuff.
		/// ie. transform parameter should be one parser for all
		/// </summary>
		public Dictionary<string, UIObject> LoadResources(
			Dictionary<string,object> dict, 
		    GameObject                gameObject = null)
		{
			//parse list of objects
			if(dict.ContainsKey("objects"))
			{

				// Log.Debug("Number of objects:"+((List<object>)dict["objects"]).Count);

				Dictionary<string, UIObject> screenObjects = new Dictionary<string, UIObject>();
			
				foreach(Dictionary<string,object> obj
					in ((List<object>)dict["objects"]))
				{
					 
					//Log.Debug("objects:"+obj["id"].ToString()+" type:"+obj["type"].ToString());
	

					if(obj.ContainsKey("type") 
						&& obj.ContainsKey("id"))
					{
						//string objId = obj["id"].ToString();
						
						//get id and prefix it by parent name
						string objId = obj["id"].ToString();						
						if(gameObject!=null) objId = gameObject.name+"."+objId;

						
						string objType = obj["type"].ToString();						
						// Log.Debug("Create ui object:"+objId+":"+objType);

						
						//create objects based on types
						if(objType == "image")
						{
							
							// Log.Debug("Create Image:"+objId);
							//TODO add creation of an image gameObject here or there?
							UIImage image = new UIImage(obj);
							if(gameObject)
								image.GameObject.transform.parent = gameObject.transform;

							image.Name = objId;
							//images.Add(objId, image);
							images.Add(image.GameObject.GetInstanceID(), image);

							screenObjects.Add(obj["id"].ToString(),image);


						}
						else if(objType == "button")
						{
							
							Log.Debug("Create Button:<color=yellow>"+objId+"</color>");
							UIButton button = new UIButton(obj);
							if(gameObject)
							{
								button.GameObject.transform.parent = gameObject.transform;
								button.Name = objId;
							}


							//images.Add(objId, button);
							images.Add(button.GameObject.GetInstanceID(), button);
							screenObjects.Add(obj["id"].ToString(),button);

						}

						else if(objType == "togglebutton" )
						{
							Log.Debug("Create Toggle Button:<color=yellow>"+objId+"</color>");
							UIToggleButton button = new UIToggleButton(obj);
							if(gameObject)
							{
								button.GameObject.transform.parent = gameObject.transform;
								button.Name = objId;
							}

							//TODO FIX_TOGGLE this hack! move button init to Settings.cs to Enter()
							// for SoundButton:
							// 	bool mutedMusic = AUDIO.AudioManager.Instance.MuteMusic;
							// 	bool mutedSfx = AUDIO.AudioManager.Instance.MuteSfx;
							// 	if(((mutedMusic) && (objId == "Settings.btn_Sound")) ||		// sets Sound button
							// 	   ((mutedSfx) && (objId == "Settings.btn_SoundEffects")))	// sets SoundEffects button
							// 	{
							// 		//button.SoundState = UISoundButton.UISoundButtonState.SOUNDOFF;
							// 		//button.SaveState = "Off";
							// 		button.Active = false;
							// 	}

							//images.Add(objId, button);
							images.Add(button.GameObject.GetInstanceID(), button);
							screenObjects.Add(obj["id"].ToString(),button);

						}
						else if(objType == "text")
						{
							
							Log.Debug("Create TEXT:<color=yellow>"+objId+"</color>");
							
							GameObject txt = new GameObject();
							txt.SetActive(false);
							txt.name = objId;
							UIText uiText = txt.AddComponent<UIText>() as UIText;
							uiText.Parse(obj);

							if(gameObject)
								txt.transform.parent = gameObject.transform;


							//images.Add(objId, text);
							txt.SetActive(true);
							screenObjects.Add(obj["id"].ToString(),uiText);

						}
						else if(objType == "popup")
						{
							//set prefab default to id and reset if defined							
							string prefab = (obj.ContainsKey("prefab")) 
								? obj["prefab"].ToString() : obj["id"].ToString();
							
							GameObject popup = GetPopup(obj["id"].ToString(), prefab);
							popup.SetActive(true);

							if(obj.ContainsKey("position"))
							{
								
								string pos = obj["position"].ToString();
								float px = System.Convert.ToSingle(pos.Split(',')[0]);
								float py = System.Convert.ToSingle(pos.Split(',')[1]);
								
								Log.Debug("HUD "+objId+" Position:"+px+","+py);
								popup.transform.position = new Vector3(px, py, 0);
								//this.gameObject.name = dict["position"].ToString();
							}
							if(obj.ContainsKey("rotation"))
							{
								
								string rot = obj["rotation"].ToString();
								float rx = System.Convert.ToSingle(rot.Split(',')[0]);
								float ry = System.Convert.ToSingle(rot.Split(',')[1]);
								
								//Log.Debug("Rotation:"+rot+":"+rx+","+ry);
								popup.transform.localRotation = Quaternion.Euler(new Vector3(rx, ry, 0));
							}
							if(obj.ContainsKey("scale"))
							{
								
								string sc = obj["scale"].ToString();
								float sx = System.Convert.ToSingle(sc.Split(',')[0]);
								float sy = System.Convert.ToSingle(sc.Split(',')[1]);
								
								popup.transform.localScale = new Vector3(sx, sy, 0);
								Log.Debug("Scale:"+sx+","+sy);
							}
							if(dict.ContainsKey("layer"))
							{
								string layer = dict["layer"].ToString();
								popup.layer = LayerMask.NameToLayer(layer);
								ChangeLayers(popup, layer);
								
							}
						}
						else if(objType == "hud")
						{

							//set prefab default to id and reset if defined							
							string prefab = (obj.ContainsKey("prefab")) 
									? obj["prefab"].ToString() : obj["id"].ToString();
							
							//Log.Debug("Create HUD as:"+prefab);
							GameObject hud = GetHud(obj["id"].ToString(), prefab);
							hud.SetActive(true);

							//Log.Warning("dict"+dict["layer"].ToString());
							if(obj.ContainsKey("position"))
							{

								string pos = obj["position"].ToString();
								float px = System.Convert.ToSingle(pos.Split(',')[0]);
								float py = System.Convert.ToSingle(pos.Split(',')[1]);

								Log.Debug("HUD "+objId+" Position:"+px+","+py);
								hud.transform.position = new Vector3(px, py, 0);
								//this.gameObject.name = dict["position"].ToString();
							}
							if(obj.ContainsKey("rotation"))
							{

								string rot = obj["rotation"].ToString();
								float rx = System.Convert.ToSingle(rot.Split(',')[0]);
								float ry = System.Convert.ToSingle(rot.Split(',')[1]);

								//Log.Debug("Rotation:"+rot+":"+rx+","+ry);
								hud.transform.localRotation = Quaternion.Euler(new Vector3(rx, ry, 0));
							}
							if(obj.ContainsKey("scale"))
							{
								string[] sc = obj["scale"].ToString().Split(',');
								float sx = System.Convert.ToSingle(sc[0]);
								float sy = System.Convert.ToSingle(sc[1]);
								float sz = 0;
								if (sc.Length > 2)
								{
									sz = System.Convert.ToSingle(sc[2]);
								}

								hud.transform.localScale = new Vector3(sx, sy, sz);
								Log.Debug(string.Format("Scale: {0}, {1}, {2}", sx, sy, sz));
							}
							// if(obj.ContainsKey("order"))
							// {
							// 	int order = System.Convert.ToInt32(obj["order"].ToString());
							// 	SortingOrder = order;

							// }
							if(obj.ContainsKey("layer"))
							{
								string layer = obj["layer"].ToString();
								hud.layer = LayerMask.NameToLayer(layer);
								ChangeLayers(hud, layer);

							}

							
						}
						else if(objType == "model")
						{
							
							//set prefab default to id and reset if defined							
							string prefab = (obj.ContainsKey("prefab")) 
								? obj["prefab"].ToString() : obj["id"].ToString();
							
							//Log.Debug("Create HUD as:"+prefab);
							GameObject hud = GetModel(obj["id"].ToString(), prefab);
							hud.SetActive(true);
							
							//Log.Warning("dict"+dict["layer"].ToString());
							if(obj.ContainsKey("position"))
							{
								
								string pos = obj["position"].ToString();
								float px = System.Convert.ToSingle(pos.Split(',')[0]);
								float py = System.Convert.ToSingle(pos.Split(',')[1]);
								float pz = System.Convert.ToSingle(pos.Split(',')[2]);
								
								Log.Debug("Model "+objId+" Position:"+px+","+py+","+pz);
								hud.transform.position = new Vector3(px, py, pz);
								//this.gameObject.name = dict["position"].ToString();
							}
							if(obj.ContainsKey("rotation"))
							{
								
								string rot = obj["rotation"].ToString();
								float rx = System.Convert.ToSingle(rot.Split(',')[0]);
								float ry = System.Convert.ToSingle(rot.Split(',')[1]);
								
								//Log.Debug("Rotation:"+rot+":"+rx+","+ry);
								hud.transform.localRotation = Quaternion.Euler(new Vector3(rx, ry, 0));
							}
							if(obj.ContainsKey("scale"))
							{
								
								string sc = obj["scale"].ToString();
								float sx = System.Convert.ToSingle(sc.Split(',')[0]);
								float sy = System.Convert.ToSingle(sc.Split(',')[1]);
								
								hud.transform.localScale = new Vector3(sx, sy, 0);
								Log.Debug("Scale:"+sx+","+sy);
							}
							// if(obj.ContainsKey("order"))
							// {
							// 	int order = System.Convert.ToInt32(obj["order"].ToString());
							// 	SortingOrder = order;
							
							// }
							if(obj.ContainsKey("layer"))
							{
								string layer = obj["layer"].ToString();
								hud.layer = LayerMask.NameToLayer(layer);
								ChangeLayers(hud, layer);
								
							}
							
							
						}
						else if(objType == "character")
						{
							
							Log.Debug("Create Character:<color=yellow>"+objId+"</color>");
							UICharacter uiChar = new UICharacter(obj);
							if(gameObject)
							{
								uiChar.GameObject.transform.parent = gameObject.transform;
								uiChar.Name = objId;
							}

							//TODO we might want this later
							//NPCView npcview = uiChar.GameObject.AddComponent<NPCView>();
							//npcview.id = objId;
							//npcview.image = uiChar;

							//read bubble info if any
							//TODO move this somewhere else? Should a bubble exist outside of uichar?
							if(obj.ContainsKey("bubble"))
							{
								foreach(Dictionary<string,object> bobj
									in ((List<object>)obj["bubble"]))
								{
									//TODO parse text parameter. (as text section?) This part is parsed
									//by UIText already, so we should only pass it over.
									//size (of text area), font, fontSize, anchor, alignment, wrapsize
									if(bobj.ContainsKey("position"))
									{

										string pos = bobj["position"].ToString();
										float px = System.Convert.ToSingle(pos.Split(',')[0]);
										float py = System.Convert.ToSingle(pos.Split(',')[1]);
										uiChar.bubblePosition = new Vector3(px, py, 0);
										Log.Debug("HUD "+objId+" bubble.Position:"+px+","+py);
									}

									if(bobj.ContainsKey("scale"))
									{

										string sc = bobj["scale"].ToString();
										float  sx = System.Convert.ToSingle(sc.Split(',')[0]);
										float  sy = System.Convert.ToSingle(sc.Split(',')[1]);
										uiChar.bubbleScale = new Vector3(sx, sy, 1);
										//Log.Debug("---------- bubble.scale:"+sx+","+sy);
									}

									if(bobj.ContainsKey("tail"))
									{

										uiChar.bubbleTail = bobj["tail"].ToString();
										
									}

								}
							}
							


							images.Add(uiChar.GameObject.GetInstanceID(), uiChar);
							screenObjects.Add(obj["id"].ToString(),uiChar);

						}
						else if(objType == "skit")
						{
							UISkit uiSkit = new UISkit(obj);
							if(gameObject)
							{
								uiSkit.GameObject.transform.parent = gameObject.transform;
								uiSkit.Name = objId;
							}

							if(obj.ContainsKey("bubble"))
							{
								foreach(Dictionary<string,object> bobj in ((List<object>)obj["bubble"]))
								{
									//TODO parse text parameter. (as text section?) This part is parsed
									//by UIText already, so we should only pass it over.
									//size (of text area), font, fontSize, anchor, alignment, wrapsize
									if(bobj.ContainsKey("position"))
									{

										string pos = bobj["position"].ToString();
										float px = System.Convert.ToSingle(pos.Split(',')[0]);
										float py = System.Convert.ToSingle(pos.Split(',')[1]);
										uiSkit.bubblePosition = new Vector3(px, py, 0);
									}

									if(bobj.ContainsKey("scale"))
									{

										string sc = bobj["scale"].ToString();
										float sx = System.Convert.ToSingle(sc.Split(',')[0]);
										float sy = System.Convert.ToSingle(sc.Split(',')[1]);
										uiSkit.bubbleScale = new Vector3(sx, sy, 1);
									}

									if(bobj.ContainsKey("tail"))
									{
										uiSkit.bubbleTail = bobj["tail"].ToString();
									}
								}
							}

							images.Add(uiSkit.GameObject.GetInstanceID(), uiSkit);
							screenObjects.Add(obj["id"].ToString(), uiSkit);
						}
						else if(objType == "video")
						{
							//Log.Info(" ---------------------------- -- ");
							//create canvas
							GameObject canvas = GameObject.CreatePrimitive(PrimitiveType.Quad);					
							GameObject.Destroy(canvas.GetComponent("MeshCollider"));
							canvas.name = objId;
							if(gameObject)
								canvas.transform.parent = gameObject.transform;

							UIVideo video = new UIVideo(canvas, obj);

							images.Add(video.GameObject.GetInstanceID(), video);
							screenObjects.Add(obj["id"].ToString(),video);

						}
					}
					else
					{
						Log.Error("Object missing id and/or type definition");
					}
				}

				return screenObjects;

			}

			return null;
		}//LoadResources()