Author Topic: How to get Sprite/Atlas selctor for a UISprite  (Read 4295 times)

terravires

  • Guest
How to get Sprite/Atlas selctor for a UISprite
« on: May 12, 2013, 08:25:04 AM »
Hello,

I'd like to use the NGUI atlas/sprite selector in the Unity Inspector to store either a string with the spriteName or reference to the sprite that is then passed to other NGUI buttons/etc.

I tried:

public UISprite _icon;

But that gives me a selector of GameObjects in the inspector.

Cheers

ArenMook

  • Administrator
  • Hero Member
  • *****
  • Thank You
  • -Given: 337
  • -Receive: 1171
  • Posts: 22,128
  • Toronto, Canada
    • View Profile
Re: How to get Sprite/Atlas selctor for a UISprite
« Reply #1 on: May 12, 2013, 01:04:43 PM »
public UISprite gives you UISprites, not game objects. When you drag & drop a game object onto it, whether it's accepted or not depends on whether it has a UISprite on it, and it's the UISprite reference that you then see, not a game object.

terravires

  • Guest
Re: How to get Sprite/Atlas selctor for a UISprite
« Reply #2 on: May 12, 2013, 09:50:27 PM »
What I mean is it's a text list of game object who have sprites attached.  When you pick one it just shows the game object and not the spritename.

Instead what I'm looking for is the popup or even a dropdown with all the sprites in an atlas.

Example, when you add a UISprite script as a component, you can pick the atlas then it has a popup with the preview images for all sprites in the atlas.

Ideally, what I'd like is to have that popup with the output a string so I can pass that to UISprite.spriteName.  Otherwise, I'm left with manually keeping track of filename strings that may or may not be in the current atlas.  Very messy.

Cheers
« Last Edit: May 12, 2013, 09:57:21 PM by terravires »

ricky

  • Newbie
  • *
  • Thank You
  • -Given: 1
  • -Receive: 0
  • Posts: 15
    • View Profile
Re: How to get Sprite/Atlas selctor for a UISprite
« Reply #3 on: November 07, 2016, 01:00:46 AM »

using UnityEngine;
using System.Collections;
using System.Collections.Generic;
using UnityEditor;


[CustomEditor(typeof(AchievementItem))]
public class AchievementItemEditor : Editor
{
    private SerializedProperty spriteName;


    void OnEnable()
    {
        spriteName = serializedObject.FindProperty("spriteName");
    }


    public override void OnInspectorGUI()
    {
        serializedObject.Update();
        DrawDefaultInspector();
        NGUIEditorTools.DrawSpriteField("Icon", spriteName.stringValue, NGUISettings.atlas, NGUISettings.selectedSprite, OnSprite, GUILayout.Width(120f));
        serializedObject.ApplyModifiedProperties();
    }


    void OnSprite(string val)
    {
        serializedObject.Update();
        Debug.Log("Called");
        spriteName.stringValue = val;
        serializedObject.ApplyModifiedProperties();
        Repaint();
    }
}