// GetFont #endregion #region GetSpriteData /// <summary> /// Loads a sprite image, saves to the atlas, and returns the sprite data /// </summary> /// <param name="psdAssetFolderPath"></param> /// <param name="filename"></param> /// <returns></returns> private static UISpriteData GetSpriteData(string psdAssetFolderPath, string filename) { // Create an atlas if one does not exist if (NGUISettings.atlas == null) { NGUISettings.atlas = PsdAtlasManager.CreateNewAtlas(psdAssetFolderPath); } // get the image Texture2D NewTexture = AssetDatabase.LoadAssetAtPath(string.Format("{0}/Images/{1}", psdAssetFolderPath, filename), typeof(Texture2D)) as Texture2D; if (NewTexture == null) { return(null); } // get the sprite, add to atlas if null UISpriteData NewSprite = NGUISettings.atlas.GetSprite(NewTexture.name); if (NewSprite == null) { NewSprite = AddTexture2DToAtlas(NewTexture); } // return the sprite return(NewSprite); }
// CreateInputLabel #endregion #region CreateSprite /// <summary> /// Creates a sprite /// </summary> /// <param name="xmlNode"></param> /// <param name="targetRootPanel"></param> /// <param name="lastAnchor"></param> /// <param name="lastAnchorPath"></param> /// <param name="actualOutput"></param> /// <param name="targetWidth"></param> /// <param name="targetHeight"></param> /// <param name="psdAssetFolderPath"></param> /// <returns></returns> public static GameObject CreateSprite(XMLNode xmlNode, UIPanel targetRootPanel, Transform lastAnchor, string lastAnchorPath, PsdImportedOutput actualOutput, float targetWidth, float targetHeight, string psdAssetFolderPath) { // get the widget info XmlWidgetInfo WidgetInfo = new XmlWidgetInfo(xmlNode); // create the base GO GameObject NewGo = CreateNguiChildBaseGo(WidgetInfo, targetWidth, targetHeight, targetRootPanel, lastAnchor, lastAnchorPath, actualOutput); // Create an atlas if one does not exist if (NGUISettings.atlas == null) { NGUISettings.atlas = PsdAtlasManager.CreateNewAtlas(psdAssetFolderPath); } // load the image UISpriteData WorkingSprite = GetSpriteData(psdAssetFolderPath, WidgetInfo.ImagePath); if (WorkingSprite == null) { Debug.LogWarning(string.Format("Could not load sprite: '{0}' with file name '{1}'", WidgetInfo.Name, WidgetInfo.ImagePath)); return(null); } // add a sprite UISprite SpriteWidget = NewGo.AddComponent <UISprite>(); SpriteWidget.name = string.Format("Sprite - {0}", WidgetInfo.Name); SpriteWidget.atlas = NGUISettings.atlas; SpriteWidget.spriteName = WorkingSprite.name; SpriteWidget.pivot = NGUISettings.pivot; SpriteWidget.depth = NGUITools.CalculateNextDepth(targetRootPanel.gameObject); SpriteWidget.transform.localScale = Vector3.one; SpriteWidget.transform.position = AdjustPosition(WidgetInfo.PosX, WidgetInfo.PosY, targetWidth, targetHeight, targetRootPanel); SpriteWidget.width = WorkingSprite.width; SpriteWidget.height = WorkingSprite.height; // check if this is a background sprite (size = psd) if ((targetWidth.Equals(WidgetInfo.SpriteWidth)) && (targetHeight.Equals(WidgetInfo.SpriteHeight))) { SpriteWidget.SetAnchor(NewGo.transform.parent.gameObject, 0, 0, 0, 0); } SpriteWidget.MakePixelPerfect(); return(NewGo); }
// CreateEmptyPanel #endregion #region CreateCheckbox /// <summary> /// Creates a checkbox /// </summary> /// <param name="xmlNode"></param> /// <param name="targetRootPanel"></param> /// <param name="lastAnchor"></param> /// <param name="lastAnchorPath"></param> /// <param name="actualOutput"></param> /// <param name="targetWidth"></param> /// <param name="targetHeight"></param> /// <param name="psdAssetFolderPath"></param> public static void CreateCheckBox(XMLNode xmlNode, UIPanel targetRootPanel, Transform lastAnchor, string lastAnchorPath, PsdImportedOutput actualOutput, float targetWidth, float targetHeight, string psdAssetFolderPath) { // get the widget info XmlWidgetInfo WidgetInfo = new XmlWidgetInfo(xmlNode); // create the base GO GameObject NewGO = CreateNguiChildBaseGo(WidgetInfo, targetWidth, targetHeight, targetRootPanel, lastAnchor, lastAnchorPath, actualOutput); // Create checkbox UIToggle NewToggle = NewGO.AddComponent <UIToggle>(); // Create an atlas if one does not exist if (NGUISettings.atlas == null) { NGUISettings.atlas = PsdAtlasManager.CreateNewAtlas(psdAssetFolderPath); } // load the image UISpriteData WorkingSprite = GetSpriteData(psdAssetFolderPath, WidgetInfo.Background); if (WorkingSprite == null) { Debug.LogWarning(string.Format("Could not load sprite: '{0}' with file name '{1}'", WidgetInfo.Name, WidgetInfo.ImagePath)); return; } // add a sprite UISprite SpriteWidget = NewGO.AddComponent <UISprite>(); SpriteWidget.atlas = NGUISettings.atlas; SpriteWidget.spriteName = WorkingSprite.name; SpriteWidget.pivot = NGUISettings.pivot; SpriteWidget.depth = NGUITools.CalculateNextDepth(targetRootPanel.gameObject); SpriteWidget.transform.localScale = Vector3.one; SpriteWidget.transform.position = AdjustPosition(WidgetInfo.PosX, WidgetInfo.PosY, targetWidth, targetHeight, targetRootPanel); SpriteWidget.width = WorkingSprite.width; SpriteWidget.height = WorkingSprite.height; SpriteWidget.MakePixelPerfect(); // create a child sprite to hold the Checked state UISprite ChildSpriteWidget = CreateChildSprite( NewGO, string.Format("{0} - Checked Sprite", NewGO.name), psdAssetFolderPath, WidgetInfo.Checkmark, targetRootPanel, WidgetInfo.PosX, WidgetInfo.PosY, targetWidth, targetHeight); // fail if nothing returned if (ChildSpriteWidget == null) { Debug.LogWarning(string.Format("Could not load sprite: '{0}' with file name '{1}'", WidgetInfo.Name, WidgetInfo.ImagePath)); return; } // set the toggle sprite NewToggle.activeSprite = ChildSpriteWidget; // add the box collider NGUITools.AddWidgetCollider(NewGO); }
// CreateClippingPlane #endregion #region CreateImageButton /// <summary> /// Creates an image button /// </summary> /// <param name="xmlNode"></param> /// <param name="targetRootPanel"></param> /// <param name="lastAnchor"></param> /// <param name="lastAnchorPath"></param> /// <param name="actualOutput"></param> /// <param name="targetWidth"></param> /// <param name="targetHeight"></param> /// <param name="psdAssetFolderPath"></param> /// <returns></returns> public static GameObject CreateImageButton(XMLNode xmlNode, UIPanel targetRootPanel, Transform lastAnchor, string lastAnchorPath, PsdImportedOutput actualOutput, float targetWidth, float targetHeight, string psdAssetFolderPath) { // get the widget info XmlWidgetInfo WidgetInfo = new XmlWidgetInfo(xmlNode); // Create an atlas if one does not exist if (NGUISettings.atlas == null) { NGUISettings.atlas = PsdAtlasManager.CreateNewAtlas(psdAssetFolderPath); } // get the button pressed states Dictionary <EButtonState, string> ButtonStatesDict = new Dictionary <EButtonState, string>(); if (!string.IsNullOrEmpty(WidgetInfo.ButtonPressedState)) { ButtonStatesDict.Add(EButtonState.Pressed, WidgetInfo.ButtonPressedState); } if (!string.IsNullOrEmpty(WidgetInfo.ButtonIdleState)) { ButtonStatesDict.Add(EButtonState.Idle, WidgetInfo.ButtonIdleState); } if (!string.IsNullOrEmpty(WidgetInfo.ButtonHoverState)) { ButtonStatesDict.Add(EButtonState.Hover, WidgetInfo.ButtonHoverState); } if (!string.IsNullOrEmpty(WidgetInfo.ButtonDisabledState)) { ButtonStatesDict.Add(EButtonState.Disabled, WidgetInfo.ButtonDisabledState); } // create the button GameObject NewGo = CreateNguiChildBaseGo(WidgetInfo, targetWidth, targetHeight, targetRootPanel, lastAnchor, lastAnchorPath, actualOutput); UIImageButton NewImageButton = NewGo.AddComponent <UIImageButton>(); // add the UISprite widget UISprite SpriteWidget = NewGo.AddComponent <UISprite>(); SpriteWidget.atlas = NGUISettings.atlas; SpriteWidget.pivot = NGUISettings.pivot; SpriteWidget.depth = NGUITools.CalculateNextDepth(targetRootPanel.gameObject); SpriteWidget.MakePixelPerfect(); NGUITools.AddWidgetCollider(NewImageButton.gameObject); NewImageButton.target = SpriteWidget; // set the UIImage button sprites bool WidthHeightSet = false; foreach (EButtonState ButtonKey in ButtonStatesDict.Keys) { // get the sprite, add to atlas if null UISpriteData ButtonSprite = GetSpriteData(psdAssetFolderPath, ButtonStatesDict[ButtonKey]); if (ButtonSprite == null) { continue; } // set sprite for the image button switch (ButtonKey) { case EButtonState.Idle: NewImageButton.normalSprite = ButtonSprite.name; break; case EButtonState.Pressed: NewImageButton.pressedSprite = ButtonSprite.name; break; case EButtonState.Hover: NewImageButton.hoverSprite = ButtonSprite.name; break; case EButtonState.Disabled: NewImageButton.disabledSprite = ButtonSprite.name; break; } // set dimensions, if not already set // NOTE: we check first, because setting the width/height performs a lot of calcs and redraws, so it's best to only set once if (!WidthHeightSet) { WidthHeightSet = true; SpriteWidget.width = ButtonSprite.width; SpriteWidget.height = ButtonSprite.height; } } // set the normal sprite SpriteWidget.spriteName = NewImageButton.normalSprite; return(NewGo); }