//================================= // Add loaded datas to temp values //================================= private static void SaveDatas() { // get values from object GameObject activeGO = Selection.activeGameObject; tmp_titleWindow = titleWindow = activeGO.name; FloatingBarVariableController activeGO_parentFbvc = activeGO.GetComponentInParent <FloatingBarVariableController>(); targettedGameObject = activeGO_parentFbvc.gameObject; FloatingBarController activeGO_fbc = activeGO.GetComponent <FloatingBarController>(); tmp_resourceVariable = resourceVariable = activeGO_fbc.resource; tmp_minResourceVariable = minResourceVariable = activeGO_fbc.Min; tmp_maxResourceVariable = maxResourceVariable = activeGO_fbc.Max; tmp_regenRate = regenRate = activeGO_parentFbvc.RegenRate; RectTransform activeGO_transform = activeGO_fbc.GetComponent <RectTransform>(); tmp_posX = posX = activeGO_transform.position.x; tmp_posY = posY = activeGO_transform.position.y; tmp_posZ = posZ = activeGO_transform.position.z; Image activeGO_fgdImage = activeGO.GetComponentsInChildren <Image>()[0]; Image activeGO_bgdImage = activeGO.GetComponentsInChildren <Image>()[1]; tmp_positiveColor = positiveColor = activeGO_bgdImage.color; tmp_negativeColor = negativeColor = activeGO_fgdImage.color; tmp_positiveImage = positiveImage = activeGO_bgdImage.sprite; tmp_negativeImage = negativeImage = activeGO_fgdImage.sprite; }
//====================================== // Restore stored data from temp values //====================================== private void RestoreDatas() { GameObject activeGO = Selection.activeGameObject; titleWindow = tmp_titleWindow; FloatingBarVariableController activeGO_parentFbvc = activeGO.GetComponentInParent <FloatingBarVariableController>(); FloatingBarController activeGO_fbc = activeGO.GetComponent <FloatingBarController>(); activeGO_fbc.resource = resourceVariable = tmp_resourceVariable; activeGO_fbc.Min = minResourceVariable = tmp_minResourceVariable; activeGO_fbc.Max = maxResourceVariable = tmp_maxResourceVariable; activeGO_parentFbvc.RegenRate = regenRate = tmp_regenRate; RectTransform activeGO_transform = activeGO_fbc.GetComponent <RectTransform>(); // We can't change x, y, z values alone activeGO_transform.position = new Vector3(tmp_posX, tmp_posY, tmp_posZ); posX = tmp_posX; posY = tmp_posY; posZ = tmp_posZ; Image activeGO_fgdImage = activeGO.GetComponentsInChildren <Image>()[0]; Image activeGO_bgdImage = activeGO.GetComponentsInChildren <Image>()[1]; activeGO_bgdImage.color = positiveColor = tmp_positiveColor; activeGO_fgdImage.color = negativeColor = tmp_negativeColor; activeGO_bgdImage.sprite = positiveImage = tmp_positiveImage; activeGO_fgdImage.sprite = negativeImage = tmp_negativeImage; }
//======================================== // Add new bar and main object connector //======================================== private void AddGeneratedBar() { // Load Floating Bar Prefab and add it to target GameObject prefab = AssetDatabase.LoadAssetAtPath("Assets/FloatingBars/Prefabs/FloatingBar Container.prefab", typeof(GameObject)) as GameObject; GameObject floatingBarPrefab = PrefabUtility.InstantiatePrefab(prefab) as GameObject; // Modify elements with given values floatingBarPrefab.name = titleWindow; FloatingBarController floatingBarController = floatingBarPrefab.GetComponent <FloatingBarController>(); floatingBarController.resource = resourceVariable; floatingBarController.Min = minResourceVariable; floatingBarController.Max = maxResourceVariable; Image backgroundImage = floatingBarPrefab.GetComponentsInChildren <Image>()[0]; backgroundImage.color = negativeColor; backgroundImage.sprite = positiveImage; Image foregroundImage = floatingBarPrefab.GetComponentsInChildren <Image>()[1]; foregroundImage.color = positiveColor; foregroundImage.sprite = negativeImage; floatingBarPrefab.transform.SetParent(targettedGameObject.transform); // Change local position after assigning to parent to avoid misplacement floatingBarPrefab.GetComponent <RectTransform>().localPosition = new Vector3(posX, posY, posZ); // Add floating bar variable controller FloatingBarVariableController fbv_controller = targettedGameObject.AddComponent <FloatingBarVariableController>(); fbv_controller.currentValue = resourceVariable; fbv_controller.resetValue = true; // default to true ... to define fbv_controller.startingValue = maxResourceVariable; fbv_controller.RegenRate = regenRate; }
//===================================================== // Only for EditMode, 'immediately' apply changes made //===================================================== private void ApplyModifications() { GameObject activeGO = Selection.activeGameObject; activeGO.name = titleWindow; FloatingBarVariableController activeGO_parentFbvc = activeGO.GetComponentInParent <FloatingBarVariableController>(); FloatingBarController activeGO_fbc = activeGO.GetComponent <FloatingBarController>(); activeGO_fbc.resource = resourceVariable; activeGO_fbc.Min = minResourceVariable; activeGO_fbc.Max = maxResourceVariable; activeGO_parentFbvc.RegenRate = regenRate; RectTransform activeGO_transform = activeGO_fbc.GetComponent <RectTransform>(); activeGO_transform.position = new Vector3(posX, posY, posZ); Image activeGO_fgdImage = activeGO.GetComponentsInChildren <Image>()[0]; Image activeGO_bgdImage = activeGO.GetComponentsInChildren <Image>()[1]; activeGO_bgdImage.color = positiveColor; activeGO_fgdImage.color = negativeColor; activeGO_bgdImage.sprite = positiveImage; activeGO_fgdImage.sprite = negativeImage; }