Ejemplo n.º 1
0
    public static UGUIAtlas LoadResources(string atlasName)
    {
        string atlasPath = "Atlas/" + atlasName;

        UGUIAtlas  atlas   = null;
        ResUseMode useMode = ResourceManager.GetResourceUseMode(atlasPath);

        switch (useMode)
        {
        case ResUseMode.RESOURCE:
            atlas = Resources.Load <UGUIAtlas> (atlasPath);
            break;

        case ResUseMode.PERSIS_STATIC_RESOURCE:
        {
            AssetBundle ab = ResourceManager.Instance.staticAssetBundleManager.LoadAssetBundle(atlasPath + ".ab");
            atlas = ab.LoadAsset <UGUIAtlas> (atlasName.ToLower());
            ResourceManager.Instance.staticAssetBundleManager.UnLoadAssetBundle(atlasPath + ".ab");
        }
        break;

        case ResUseMode.PERSIS_DYNAMIC_RESOURCE:
        {
            AssetBundle ab = ResourceManager.Instance.dynamicAssetBundleManager.LoadAssetBundle(atlasPath + ".ab");
            atlas = ab.LoadAsset <UGUIAtlas> (atlasName.ToLower());
            ResourceManager.Instance.staticAssetBundleManager.UnLoadAssetBundle(atlasPath + ".ab");
        }
        break;
        }

        return(atlas);
    }
Ejemplo n.º 2
0
    static public void Show(UGUIAtlas _atlas, Action <Sprite> _cb)
    {
        SpriteSelector comp = ScriptableWizard.DisplayWizard <SpriteSelector>("选择图片");

        comp.mCb      = _cb;
        comp.mSprites = _atlas.SpriteList;
    }
Ejemplo n.º 3
0
    /// <summary>
    /// Draw the atlas and sprite selection fields.
    /// </summary>

    protected virtual bool ShouldDrawProperties()
    {
        GUILayout.BeginHorizontal();
        if (UGUIEditorTools.DrawPrefixButton("Atlas"))
        {
            ComponentSelector.Show <UGUIAtlas>(OnSelectAtlas);
        }
        SerializedProperty atlas = UGUIEditorTools.DrawProperty("", serializedObject, "mAtlas", GUILayout.MinWidth(20f));

        if (GUILayout.Button("Edit", GUILayout.Width(40f)))
        {
            if (atlas != null)
            {
                UGUIAtlas atl = atlas.objectReferenceValue as UGUIAtlas;
                UGUISettings.atlas = atl;
                UGUIEditorTools.Select(atl.gameObject);
            }
        }
        GUILayout.EndHorizontal();

        UGUISprite uguiSprite = target as UGUISprite;
        string     spriteName = uguiSprite.sprite == null ? "" : uguiSprite.sprite.name;

        UGUIEditorTools.DrawAdvancedSpriteField(atlas.objectReferenceValue as UGUIAtlas, spriteName, SelectSprite, true);
        return(true);
    }
Ejemplo n.º 4
0
    private static void BuildOnceUGUIAtlas(DirectoryInfo dirInfo)
    {
        string spriteDir = Application.dataPath + "/Client/Resources/Atlas";

        if (!Directory.Exists(spriteDir))
        {
            Directory.CreateDirectory(spriteDir);
        }

        string atlasName      = dirInfo.Name;
        string prefabFullPath = spriteDir + "/" + atlasName + ".prefab";
        string prefabPath     = prefabFullPath.Substring(prefabFullPath.IndexOf("Assets"));


        GameObject go    = new GameObject(atlasName);
        UGUIAtlas  atlas = go.AddComponent <UGUIAtlas> ();


        foreach (FileInfo pngFile in dirInfo.GetFiles("*.png", SearchOption.AllDirectories))
        {
            string allPath   = pngFile.FullName;
            string assetPath = allPath.Substring(allPath.IndexOf("Assets"));
            Sprite sprite    = AssetDatabase.LoadAssetAtPath <Sprite>(assetPath);
            atlas.AddSprite(sprite);
        }
        PrefabUtility.CreatePrefab(prefabPath, go);
        GameObject.DestroyImmediate(go);
    }
Ejemplo n.º 5
0
 public Dictionary <string, Sprite> getSprites(UGUIAtlas atlas)
 {
     if (mSpriteList.TryGetValue(atlas, out Dictionary <string, Sprite> spriteList))
     {
         return(spriteList);
     }
     return(null);
 }
Ejemplo n.º 6
0
    /// <summary>
    /// Helper function that determines whether the two atlases are related.
    /// </summary>

    static public bool CheckIfRelated(UGUIAtlas a, UGUIAtlas b)
    {
        if (a == null || b == null)
        {
            return(false);
        }
        return(a == b || a.References(b) || b.References(a));
    }
Ejemplo n.º 7
0
    /// <summary>
    /// Helper function that determines whether the atlas uses the specified one, taking replacements into account.
    /// </summary>

    bool References(UGUIAtlas atlas)
    {
        if (atlas == null)
        {
            return(false);
        }
        if (atlas == this)
        {
            return(true);
        }
        return((mReplacement != null) ? mReplacement.References(atlas) : false);
    }
Ejemplo n.º 8
0
 public override void setAtlas(UGUIAtlas atlas, bool clearSprite = false)
 {
     if (atlas == getAtlas())
     {
         return;
     }
     // 改变图集时先停止播放
     stop();
     base.setAtlas(atlas, clearSprite);
     // 图集改变后清空当前序列帧列表
     setTextureSet(null);
 }
Ejemplo n.º 9
0
    public void UnLoadAtlas(string atlasName)
    {
        UGUIAtlas sp = null;

        if (spritePackers.TryGetValue(atlasName, out sp))
        {
            sp.Destory();
        }


        spritePackers.Remove(atlasName);
    }
Ejemplo n.º 10
0
    public override void init()
    {
        base.init();
        // 获取image组件,如果没有则添加,这样是为了使用代码新创建一个image窗口时能够正常使用image组件
        mImage = mObject.GetComponent <Image>();
        if (mImage == null)
        {
            mImage = mObject.AddComponent <Image>();
            // 添加UGUI组件后需要重新获取RectTransform
            mRectTransform = mObject.GetComponent <RectTransform>();
            mTransform     = mRectTransform;
        }
        if (mImage == null)
        {
            logError(Typeof(this) + " can not find " + Typeof <Image>() + ", window:" + mName + ", layout:" + mLayout.getName());
        }
        mOriginSprite = mImage.sprite;
        // 获取初始的精灵所在图集
        if (mOriginSprite != null)
        {
            Texture2D curTexture = mOriginSprite.texture;
            if (curTexture != null)
            {
                ImageAtlasPath imageAtlasPath = mObject.GetComponent <ImageAtlasPath>();
                string         atlasPath;
                if (imageAtlasPath != null)
                {
                    atlasPath = imageAtlasPath.mAtlasPath;
                }
                else
                {
                    atlasPath = FrameDefine.R_ATLAS_GAME_ATLAS_PATH + curTexture.name + "/" + curTexture.name;
                }
                mAtlas = mTPSpriteManager.getAtlas(atlasPath, false, true);
                if (mAtlas != null && mAtlas.mTexture != curTexture)
                {
                    logError("设置的图集与加载出的图集不一致!可能未添加ImageAtlasPath组件,或者ImageAtlasPath组件中记录的路径错误,或者是在当前物体在重复使用过程中销毁了原始图集");
                }
            }
        }
        mOriginTextureName = getSpriteName();
        string materialName = getMaterialName();

        // 不再将默认材质替换为自定义的默认材质,只判断其他材质
        if (!isEmpty(materialName) && materialName != FrameDefine.BUILDIN_UI_MATERIAL)
        {
            if (!mShaderManager.isSingleShader(materialName))
            {
                setMaterialName(materialName, true);
            }
        }
    }
Ejemplo n.º 11
0
        public override void OnInspectorGUI()
        {
            UGUIAtlas atlas = target as UGUIAtlas;

            atlas.MainTex = EditorGUILayout.ObjectField("MainTextture", atlas.MainTex, typeof(Texture2D), true) as Texture2D;

            if (GUILayout.Button("Refresh"))
            {
                if (atlas.MainTex == null)
                {
                    string path = EditorUtility.OpenFilePanel("Select a Sprite", Application.dataPath, "png");
                    if (!string.IsNullOrEmpty(path))
                    {
                        atlas.MainTex = AssetDatabase.LoadAssetAtPath(path, typeof(Texture2D)) as Texture2D;
                    }
                }

                if (atlas.MainTex != null)
                {
                    string          path     = AssetDatabase.GetAssetPath(atlas.MainTex);
                    TextureImporter importer = AssetImporter.GetAtPath(path) as TextureImporter;
                    if (importer != null && importer.textureType == TextureImporterType.Sprite && importer.spriteImportMode == SpriteImportMode.Multiple)
                    {
                        Object[] objs = AssetDatabase.LoadAllAssetsAtPath(AssetDatabase.GetAssetPath(atlas.MainTex));
                        atlas.SpriteLists.Clear();
                        foreach (Object o in objs)
                        {
                            if (o.GetType() == typeof(Texture2D))
                            {
                                atlas.MainTex = o as Texture2D;
                            }
                            else if (o.GetType() == typeof(Sprite))
                            {
                                atlas.SpriteLists.Add(o as Sprite);
                            }
                        }
                    }
                    else
                    {
                        atlas.MainTex = null;
                    }
                }
            }

            if (atlas.SpriteLists.Count > 0)
            {
                foreach (Sprite s in atlas.SpriteLists)
                {
                    EditorGUILayout.ObjectField(s.name, s, typeof(Sprite), true);
                }
            }
        }
Ejemplo n.º 12
0
 public Sprite getSprite(UGUIAtlas atlas, string spriteName)
 {
     if (atlas == null || spriteName == null)
     {
         return(null);
     }
     if (mSpriteList.TryGetValue(atlas, out Dictionary <string, Sprite> spriteList) &&
         spriteList.TryGetValue(spriteName, out Sprite sprite))
     {
         return(sprite);
     }
     return(null);
 }
Ejemplo n.º 13
0
 public static void Show(UGUIAtlas atlas, UnityAction <Sprite> onClickSprite = null, UnityAction <Sprite> onDoubleClickSprite = null, Sprite currentSprite = null)
 {
     if (instance != null)
     {
         instance.Close();
         instance = null;
     }
     instance                     = ScriptableWizard.DisplayWizard <AtlasSpriteSelector> ("Select a Sprite");
     instance.targetAtlas         = atlas;
     instance.onClickSprite       = onClickSprite;
     instance.onDoubleClickSprite = onDoubleClickSprite;
     instance.currentSprite       = currentSprite;
 }
Ejemplo n.º 14
0
    private void SerialiseSprites()
    {
        string    _Path       = _sheetUnityPath + ".png";
        string    _atlasPath  = _sheetUnityPath + ".prefab";
        UGUIAtlas _atlasClass = null;

        if (File.Exists(_atlasPath))
        {
            GameObject basePrebab       = AssetDatabase.LoadAssetAtPath(_atlasPath, typeof(GameObject)) as GameObject;
            GameObject prefabGameobject = PrefabUtility.InstantiatePrefab(basePrebab) as GameObject;
            if (prefabGameobject)
            {
                _atlasClass            = prefabGameobject.GetComponent <UGUIAtlas>();
                _atlasClass.Atlas      = AssetDatabase.LoadAssetAtPath <Texture>(_Path);
                _atlasClass.AllSprites = new List <Sprite>();
                Object[] _allAsset = AssetDatabase.LoadAllAssetsAtPath(_Path);
                for (int i = 0; i < _allAsset.Length; i++)
                {
                    if (!(_allAsset[i] is Sprite))
                    {
                        continue;
                    }
                    Sprite _spt = _allAsset[i] as Sprite;
                    _atlasClass.AllSprites.Add(_spt);
                }
            }
            PrefabUtility.ReplacePrefab(prefabGameobject, basePrebab, ReplacePrefabOptions.Default);
            MonoBehaviour.DestroyImmediate(prefabGameobject);
        }
        else
        {
            GameObject _prefab = new GameObject(_folderName);
            _atlasClass            = _prefab.AddComponent <UGUIAtlas>();
            _atlasClass.Atlas      = AssetDatabase.LoadAssetAtPath <Texture>(_Path);
            _atlasClass.AllSprites = new List <Sprite>();
            Object[] _allAsset = AssetDatabase.LoadAllAssetsAtPath(_Path);
            for (int i = 0; i < _allAsset.Length; i++)
            {
                if (!(_allAsset[i] is Sprite))
                {
                    continue;
                }
                Sprite _spt = _allAsset[i] as Sprite;
                _atlasClass.AllSprites.Add(_spt);
            }
            PrefabUtility.CreatePrefab(_atlasPath, _prefab, ReplacePrefabOptions.ConnectToPrefab);
            MonoBehaviour.DestroyImmediate(_prefab);
        }
    }
Ejemplo n.º 15
0
 static int LoadResources(IntPtr L)
 {
     try
     {
         ToLua.CheckArgsCount(L, 1);
         string    arg0 = ToLua.CheckString(L, 1);
         UGUIAtlas o    = UGUIAtlasManager.LoadResources(arg0);
         ToLua.Push(L, o);
         return(1);
     }
     catch (Exception e)
     {
         return(LuaDLL.toluaL_exception(L, e));
     }
 }
Ejemplo n.º 16
0
 public virtual void setAtlas(UGUIAtlas atlas, bool clearSprite = false)
 {
     if (mImage == null)
     {
         return;
     }
     mAtlas = atlas;
     if (clearSprite)
     {
         setSprite(null);
     }
     else
     {
         setSprite(mTPSpriteManager.getSprite(mAtlas, getSpriteName()));
     }
 }
Ejemplo n.º 17
0
    /// <summary>
    /// Replacement atlas selection callback.
    /// </summary>

    void OnSelectAtlas(Object obj)
    {
        if (mReplacement != obj)
        {
            // Undo doesn't work correctly in this case... so I won't bother.
            //UGUIEditorTools.RegisterUndo("Atlas Change");
            //UGUIEditorTools.RegisterUndo("Atlas Change", mAtlas);

            mAtlas.replacement = obj as UGUIAtlas;
            mReplacement       = mAtlas.replacement;
            UGUITools.SetDirty(mAtlas);
            if (mReplacement == null)
            {
                mType = AtlasType.Normal;
            }
        }
    }
Ejemplo n.º 18
0
    /// <summary>
    /// Show the sprite selection wizard.
    /// </summary>

    static public void Show(SerializedObject ob, SerializedProperty pro, UGUIAtlas atlas)
    {
        if (instance != null)
        {
            instance.Close();
            instance = null;
        }

        if (ob != null && pro != null && atlas != null)
        {
            SpriteSelector comp = ScriptableWizard.DisplayWizard <SpriteSelector>("Select a Sprite");
            UGUISettings.atlas          = atlas;
            UGUISettings.selectedSprite = pro.hasMultipleDifferentValues ? null : pro.stringValue;
            comp.mSprite   = null;
            comp.mObject   = ob;
            comp.mProperty = pro;
            comp.mCallback = comp.OnSpriteSelection;
        }
    }
Ejemplo n.º 19
0
    // 图集资源已经加载完成,从assets中解析并创建图集信息
    protected UGUIAtlas atlasLoaded <T>(T[] assets, string loadPath) where T : UnityEngine.Object
    {
        if (assets == null || assets.Length == 0)
        {
            return(null);
        }
        Texture2D texture = null;
        // 找到Texture,位置不一定是第一个,需要遍历查找
        int count = assets.Length;

        for (int i = 0; i < count; ++i)
        {
            T item = assets[i];
            if (item is Texture2D)
            {
                texture = item as Texture2D;
                break;
            }
        }
        if (texture == null)
        {
            logError("can not find texture2D in loaded Texture:" + loadPath);
        }
        // 找出所有的精灵
        var spriteList = new Dictionary <string, Sprite>();

        for (int i = 0; i < count; ++i)
        {
            T item = assets[i];
            if (item is Sprite)
            {
                spriteList.Add(item.name, item as Sprite);
            }
        }
        UGUIAtlas atlas = new UGUIAtlas();

        atlas.mTexture = texture;
        mSpriteNameList.Add(loadPath, spriteList);
        mSpriteList.Add(atlas, spriteList);
        mAtlasNameList.Add(loadPath, atlas);
        mAtlasList.Add(atlas, loadPath);
        return(atlas);
    }
Ejemplo n.º 20
0
    private void OnDoubleClickAtlas(UGUIAtlas atlas)
    {
        serializedObject.Update();
        SerializedProperty atlasProperty = serializedObject.FindProperty("_uiAtlas");

        if (atlasProperty != null && atlasProperty.objectReferenceValue != atlas)
        {
            Undo.RecordObject(serializedObject.targetObject, "Atlas Selection");

            atlasProperty.objectReferenceValue = atlas;
            SerializedProperty spriteProperty = serializedObject.FindProperty("m_Sprite");
            if (spriteProperty != null)
            {
                spriteProperty.objectReferenceValue = null;
            }
            serializedObject.ApplyModifiedProperties();
            EditorUtility.SetDirty(serializedObject.targetObject);
        }
        AtlasSelector.Hide();
    }
Ejemplo n.º 21
0
    private void LoadAllAtlases()
    {
        if (atlases == null)
        {
            atlases = new List <UGUIAtlas> ();
        }
        else
        {
            atlases.Clear();
        }

        string[]      paths     = AssetDatabase.GetAllAssetPaths();
        List <string> assetList = new List <string> ();

        for (int i = 0; i < paths.Length; i++)
        {
            if (paths [i].EndsWith(".prefab", StringComparison.OrdinalIgnoreCase) == true)
            {
                assetList.Add(paths [i]);
            }
        }

        for (int i = 0; i < assetList.Count; i++)
        {
            EditorUtility.DisplayProgressBar("Searching", "Searching atlases, please wait...", (float)i / assetList.Count);
            UnityEngine.Object target = AssetDatabase.LoadMainAssetAtPath(assetList [i]);
            if (target != null && target is GameObject)
            {
                UGUIAtlas atlas = (target as GameObject).GetComponent <UGUIAtlas> ();
                if (atlas != null)
                {
                    atlases.Add(atlas);
                }
            }
        }
        EditorUtility.ClearProgressBar();

        SortAtlases();
    }
Ejemplo n.º 22
0
        public static void CreateAtlasPrefab()
        {
            Object activeObj = Selection.activeObject;

            if (activeObj == null)
            {
                Debug.LogError("Please select sprite atlas.");
                return;
            }
            string          path     = AssetDatabase.GetAssetPath(activeObj);
            TextureImporter importer = AssetImporter.GetAtPath(path) as TextureImporter;

            if (importer != null && importer.textureType == TextureImporterType.Sprite && importer.spriteImportMode == SpriteImportMode.Multiple)
            {
                UGUIAtlas atlas = ScriptableObject.CreateInstance <UGUIAtlas>();
                object[]  objs  = AssetDatabase.LoadAllAssetsAtPath(path);
                atlas.SpriteLists.Clear();
                foreach (object o in objs)
                {
                    if (o.GetType() == typeof(Texture2D))
                    {
                        atlas.MainTex = o as Texture2D;
                    }
                    else if (o.GetType() == typeof(Sprite))
                    {
                        atlas.SpriteLists.Add(o as Sprite);
                    }
                }
                AssetDatabase.CreateAsset(atlas, path.Replace(".png", "_Atlas.asset"));
                AssetDatabase.Refresh();
            }
            else
            {
                Debug.LogError("Select not a atlas.");
            }
        }
Ejemplo n.º 23
0
    /// <summary>
    /// Draw the custom wizard.
    /// </summary>

    void OnGUI()
    {
        EditorGUIUtility.labelWidth = 80;

        if (UGUISettings.atlas == null)
        {
            GUILayout.Label("No Atlas selected.", "LODLevelNotifyText");
        }
        else
        {
            UGUIAtlas atlas = UGUISettings.atlas;
            bool      close = false;
            GUILayout.Label(atlas.name + " Sprites", "LODLevelNotifyText");
            UGUIEditorTools.DrawSeparator();

            GUILayout.BeginHorizontal();
            GUILayout.Space(84f);

            string before = UGUISettings.partialSprite;
            string after  = EditorGUILayout.TextField("", before, "SearchTextField");
            if (before != after)
            {
                UGUISettings.partialSprite = after;
            }

            if (GUILayout.Button("", "SearchCancelButton", GUILayout.Width(18f)))
            {
                UGUISettings.partialSprite = "";
                GUIUtility.keyboardControl = 0;
            }
            GUILayout.Space(84f);
            GUILayout.EndHorizontal();

//			Texture2D tex = atlas.mainTexture as Texture2D;
//
//			if (tex == null)
//			{
//				GUILayout.Label("The atlas doesn't have a texture to work with");
//				return;
//			}

            BetterList <string> sprites = atlas.GetListOfSprites(UGUISettings.partialSprite);

            float size   = 80f;
            float padded = size + 10f;

            int columns = Mathf.FloorToInt(Screen.width / padded) / 2;
            if (columns < 1)
            {
                columns = 1;
            }
//			Debug.LogWarning (columns);

            int  offset = 0;
            Rect rect   = new Rect(10f, 0, size, size);

            GUILayout.Space(10f);
            mPos = GUILayout.BeginScrollView(mPos);
            int rows = 1;

            while (offset < sprites.size)
            {
                GUILayout.BeginHorizontal();
                {
                    int col = 0;
                    rect.x = 10f;

                    for (; offset < sprites.size; ++offset)
                    {
                        Sprite sprite = atlas.GetSprite(sprites[offset]);
                        if (sprite == null)
                        {
                            continue;
                        }

                        // Button comes first
                        if (GUI.Button(rect, ""))
                        {
                            if (Event.current.button == 0)
                            {
                                float delta = Time.realtimeSinceStartup - mClickTime;
                                mClickTime = Time.realtimeSinceStartup;

                                if (UGUISettings.selectedSprite != sprite.name)
                                {
                                    if (mSprite != null)
                                    {
                                        UGUIEditorTools.RegisterUndo("Atlas Selection", mSprite);
                                        mSprite.SetNativeSize();
                                        EditorUtility.SetDirty(mSprite.gameObject);
                                    }

                                    UGUISettings.selectedSprite = sprite.name;
                                    UGUIEditorTools.RepaintSprites();
                                    if (mCallback != null)
                                    {
                                        mCallback(sprite.name);
                                    }
                                }
                                else if (delta < 0.5f)
                                {
                                    close = true;
                                }
                            }
                            else
                            {
//								NGUIContextMenu.AddItem("Edit", false, EditSprite, sprite);
//								NGUIContextMenu.AddItem("Delete", false, DeleteSprite, sprite);
//								NGUIContextMenu.Show();
                            }
                        }

                        if (Event.current.type == EventType.Repaint)
                        {
                            // On top of the button we have a checkboard grid
                            UGUIEditorTools.DrawTiledTexture(rect, UGUIEditorTools.backdropTexture);
//							Rect uv = new Rect(sprite.rect.x, sprite.rect.y, sprite.rect.width, sprite.rect.height);
//							uv = UGUIMath.ConvertToTexCoords(uv, tex.width, tex.height);

                            // Calculate the texture's scale that's needed to display the sprite in the clipped area
//							float scaleX = rect.width / uv.width;
//							float scaleY = rect.height / uv.height;

                            // Stretch the sprite so that it will appear proper
//							float aspect = (scaleY / scaleX) / ((float)tex.height / tex.width);
//							Rect clipRect = rect;
//
//							if (aspect != 1f)
//							{
//								if (aspect < 1f)
//								{
//									// The sprite is taller than it is wider
//									float padding = size * (1f - aspect) * 0.5f;
//									clipRect.xMin += padding;
//									clipRect.xMax -= padding;
//								}
//								else
//								{
//									// The sprite is wider than it is taller
//									float padding = size * (1f - 1f / aspect) * 0.5f;
//									clipRect.yMin += padding;
//									clipRect.yMax -= padding;
//								}
//							}

                            GUI.DrawTexture(rect, sprite.texture);

                            // Draw the selection
                            if (UGUISettings.selectedSprite == sprite.name)
                            {
                                UGUIEditorTools.DrawOutline(rect, new Color(0.4f, 1f, 0f, 1f));
                            }
                        }

                        GUI.backgroundColor = new Color(1f, 1f, 1f, 0.5f);
                        GUI.contentColor    = new Color(1f, 1f, 1f, 0.7f);
                        GUI.Label(new Rect(rect.x, rect.y + rect.height, rect.width, 32f), sprite.name, "ProgressBarBack");
                        GUI.contentColor    = Color.white;
                        GUI.backgroundColor = Color.white;

                        if (++col >= columns)
                        {
                            ++offset;
                            break;
                        }
                        rect.x += padded;
                    }
                }
                GUILayout.EndHorizontal();
                GUILayout.Space(padded);
                rect.y += padded + 26;
                ++rows;
            }
            GUILayout.Space(rows * 26);
            GUILayout.EndScrollView();

            if (close)
            {
                Close();
            }
        }
    }
Ejemplo n.º 24
0
    void OnGUI()
    {
        if (atlases == null || atlases.Count == 0)
        {
            GUILayout.Label("No Atlas found, please create an Atlas first.", "LODLevelNotifyText");
        }
        else
        {
            GUILayout.Label("Atlases in Project", "LODLevelNotifyText");
            EditorUtils.DrawSeparator();
            GUILayout.BeginHorizontal();
            highResolution = GUILayout.Toggle(highResolution, "HighResolution");
            searchName     = EditorUtils.DrawSearchBox(searchName);
            GUILayout.EndHorizontal();
            int columns = 0;
            if (highResolution == false)
            {
                columns = Mathf.FloorToInt(Screen.width / PaddedCellWidth);
            }
            else
            {
                columns = Mathf.FloorToInt(Screen.width * 0.5f / PaddedCellWidth);
            }
            columns = columns < 1 ? 1 : columns;
            int rows = (int)Mathf.CeilToInt((float)atlases.Count / columns);
            rows = rows < 1 ? 1 : rows;

            GUILayout.Space(10.0f);

            using (GUILayout.ScrollViewScope scrollViewScope = new GUILayout.ScrollViewScope(scrollPosition)) {
                scrollPosition = scrollViewScope.scrollPosition;

                GUILayout.Space(rows * PaddedCellHeight);

                for (int i = 0; i < atlases.Count; i++)
                {
                    UGUIAtlas atlas = atlases [i];
                    Rect      rect  = new Rect(PaddedCellWidth * (i % columns) + Padding, PaddedCellHeight * (i / columns) + Padding, CellSize, CellSize);

                    if (atlas == null || atlas.Atlas == null)
                    {
                        if (atlasToRemove != null)
                        {
                            atlasToRemove.Add(atlas);
                        }
                        continue;
                    }

                    if (GUI.Button(rect, ""))
                    {
                        if (Event.current.button == 0)
                        {
                            float delta = Time.realtimeSinceStartup - timer;
                            timer = Time.realtimeSinceStartup;

                            if (onClickAtlas != null)
                            {
                                onClickAtlas(atlas);
                            }

                            if (selectedAtlas == atlas)
                            {
                                if (delta < DoubleClickThreshold)
                                {
                                    if (onDoubleClickAtlas != null)
                                    {
                                        onDoubleClickAtlas(atlas);
                                    }
                                }
                            }
                            else
                            {
                                selectedAtlas = atlas;
                            }
                        }
                    }

                    if (Event.current.type == EventType.Repaint)
                    {
                        Rect  clipRect = rect;
                        float aspect   = (float)atlas.Atlas.width / atlas.Atlas.height;

                        if (aspect != 1.0f)
                        {
                            if (aspect < 1.0f)
                            {
                                float padding = CellSize * (1.0f - aspect) * 0.5f;
                                clipRect.xMin += padding;
                                clipRect.xMax -= padding;
                            }
                            else
                            {
                                float padding = CellSize * (1.0f - 1.0f / aspect) * 0.5f;
                                clipRect.yMin += padding;
                                clipRect.yMax -= padding;
                            }
                        }

                        EditorUtils.DrawContrastBackground(clipRect);
                        GUI.DrawTexture(rect, atlas.Atlas, ScaleMode.ScaleToFit);

                        if (atlas == selectedAtlas)
                        {
                            EditorUtils.DrawOutline(rect, Color.green);
                        }
                        GUI.backgroundColor = backgroundColor;
                        GUI.contentColor    = contentColor;
                        GUI.Label(new Rect(rect.x, rect.y + rect.height, rect.width, NameHeight), atlas.name, "ProgressBarBack");
                        GUI.contentColor    = Color.white;
                        GUI.backgroundColor = Color.white;
                    }
                }

                if (atlasToRemove != null && atlasToRemove.Count > 0)
                {
                    foreach (var item in atlasToRemove)
                    {
                        atlases.Remove(item);
                    }
                }
                atlasToRemove.Clear();
            }
        }
        GUILayout.FlexibleSpace();
        if (GUILayout.Button("Search Atlas", GUILayout.Height(100)) == true)
        {
            LoadAllAtlases();
        }
    }
Ejemplo n.º 25
0
    public static void Show(UnityAction <UGUIAtlas> onClickAtlas = null, UnityAction <UGUIAtlas> onDoubleClickAtlas = null, UGUIAtlas selectedAtlas = null)
    {
        if (instance != null)
        {
            instance.Close();
            instance = null;
        }

        instance = ScriptableWizard.DisplayWizard <AtlasSelector> ("Select an atlas");
        instance.onClickAtlas       = onClickAtlas;
        instance.onDoubleClickAtlas = onDoubleClickAtlas;
        instance.selectedAtlas      = selectedAtlas;
    }
Ejemplo n.º 26
0
    /// <summary>
    /// Draw the inspector widget.
    /// </summary>

    public override void OnInspectorGUI()
    {
        UGUIEditorTools.SetLabelWidth(80f);
        mAtlas = target as UGUIAtlas;

        Sprite sprite = (mAtlas != null) ? mAtlas.GetSprite(UGUISettings.selectedSprite) : null;

        GUILayout.Space(6f);

        if (mAtlas.replacement != null)
        {
            mType        = AtlasType.Reference;
            mReplacement = mAtlas.replacement;
        }

        GUILayout.BeginHorizontal();
        AtlasType after = (AtlasType)EditorGUILayout.EnumPopup("Atlas Type", mType);

        UGUIEditorTools.DrawPadding();
        GUILayout.EndHorizontal();

        if (mType != after)
        {
            if (after == AtlasType.Normal)
            {
                mType = AtlasType.Normal;
                OnSelectAtlas(null);
            }
            else
            {
                mType = AtlasType.Reference;
            }
        }

        if (mType == AtlasType.Reference)
        {
            ComponentSelector.Draw <UGUIAtlas>(mAtlas.replacement, OnSelectAtlas, true);

            GUILayout.Space(6f);
            EditorGUILayout.HelpBox("You can have one atlas simply point to " +
                                    "another one. This is useful if you want to be " +
                                    "able to quickly replace the contents of one " +
                                    "atlas with another one, for example for " +
                                    "swapping an SD atlas with an HD one, or " +
                                    "replacing an English atlas with a Chinese " +
                                    "one. All the sprites referencing this atlas " +
                                    "will update their references to the new one.", MessageType.Info);

            if (mReplacement != mAtlas && mAtlas.replacement != mReplacement)
            {
                UGUIEditorTools.RegisterUndo("Atlas Change", mAtlas);
                mAtlas.replacement = mReplacement;
                UGUITools.SetDirty(mAtlas);
            }
            return;
        }

        GUILayout.Space(6f);
//		Material mat = EditorGUILayout.ObjectField("Material", mAtlas.spriteMaterial, typeof(Material), false) as Material;
//
//		if (mAtlas.spriteMaterial != mat)
//		{
//			UGUIEditorTools.RegisterUndo("Atlas Change", mAtlas);
//			mAtlas.spriteMaterial = mat;
//
//			// Ensure that this atlas has valid import settings
//			if (mAtlas.texture != null) UGUIEditorTools.ImportTexture(mAtlas.texture, false, false, !mAtlas.premultipliedAlpha);
//
//			mAtlas.MarkAsChanged();
//		}
//
//		if (mat != null)
//		{
//			TextAsset ta = EditorGUILayout.ObjectField("TP Import", null, typeof(TextAsset), false) as TextAsset;
//
//			if (ta != null)
//			{
//				// Ensure that this atlas has valid import settings
//				if (mAtlas.texture != null) UGUIEditorTools.ImportTexture(mAtlas.texture, false, false, !mAtlas.premultipliedAlpha);
//
//				UGUIEditorTools.RegisterUndo("Import Sprites", mAtlas);
//				NGUIJson.LoadSpriteData(mAtlas, ta);
//				if (sprite != null) sprite = mAtlas.GetSprite(sprite.name);
//				mAtlas.MarkAsChanged();
//			}
//
//			float pixelSize = EditorGUILayout.FloatField("Pixel Size", mAtlas.pixelSize, GUILayout.Width(120f));
//
//			if (pixelSize != mAtlas.pixelSize)
//			{
//				UGUIEditorTools.RegisterUndo("Atlas Change", mAtlas);
//				mAtlas.pixelSize = pixelSize;
//			}
//		}

//		if (mAtlas.mainTexture != null)
        {
            Color blueColor  = new Color(0f, 0.7f, 1f, 1f);
            Color greenColor = new Color(0.4f, 1f, 0f, 1f);

            if (sprite == null && mAtlas.spriteList.Count > 0)
            {
                string spriteName = UGUISettings.selectedSprite;
                if (!string.IsNullOrEmpty(spriteName))
                {
                    sprite = mAtlas.GetSprite(spriteName);
                }
                if (sprite == null)
                {
                    sprite = mAtlas.spriteList[0];
                }
            }

            if (sprite != null)
            {
                if (sprite == null)
                {
                    return;
                }

//				Texture2D tex = mAtlas.mainTexture as Texture2D;

//				if (tex != null)
                {
                    if (!UGUIEditorTools.DrawHeader("Sprite Details"))
                    {
                        return;
                    }

                    UGUIEditorTools.BeginContents();

                    GUILayout.Space(3f);
                    UGUIEditorTools.DrawAdvancedSpriteField(mAtlas, sprite.name, SelectSprite, true);
                    GUILayout.Space(6f);

                    GUI.changed = false;

                    GUI.backgroundColor = greenColor;
                    UGUIEditorTools.IntVector sizeA = UGUIEditorTools.IntPair("Dimensions", "X", "Y", (int)sprite.textureRect.x, (int)sprite.textureRect.y);
                    UGUIEditorTools.IntVector sizeB = UGUIEditorTools.IntPair(null, "Width", "Height", (int)sprite.textureRect.width, (int)sprite.textureRect.height);

                    EditorGUILayout.Separator();
                    GUI.backgroundColor = blueColor;
                    UGUIEditorTools.IntVector borderA = UGUIEditorTools.IntPair("Border", "Left", "Right", (int)sprite.border.x, (int)sprite.border.y);
                    UGUIEditorTools.IntVector borderB = UGUIEditorTools.IntPair(null, "Bottom", "Top", (int)sprite.border.y, (int)sprite.border.z);

                    EditorGUILayout.Separator();
                    GUI.backgroundColor = Color.white;
                    UGUIEditorTools.IntVector padA = UGUIEditorTools.IntPair("Padding", "Left", "Right", (int)sprite.rect.xMin, (int)sprite.rect.xMax);
                    UGUIEditorTools.IntVector padB = UGUIEditorTools.IntPair(null, "Bottom", "Top", (int)sprite.rect.yMax, (int)sprite.rect.yMin);

                    if (GUI.changed)
                    {
//						UGUIEditorTools.RegisterUndo("Atlas Change", mAtlas);
//
//						sprite.rect.x = sizeA.x;
//						sprite.rect.y = sizeA.y;
//						sprite.rect.width = sizeB.x;
//						sprite.rect.height = sizeB.y;
//
//						sprite.paddingLeft = padA.x;
//						sprite.paddingRight = padA.y;
//						sprite.paddingBottom = padB.x;
//						sprite.paddingTop = padB.y;
//
//						sprite.borderLeft = borderA.x;
//						sprite.borderRight = borderA.y;
//						sprite.borderBottom = borderB.x;
//						sprite.borderTop = borderB.y;
//
//						MarkSpriteAsDirty();
                    }

                    GUILayout.Space(3f);

                    GUILayout.BeginHorizontal();

//					if (GUILayout.Button("Duplicate"))
//					{
//						UGUIAtlasMaker.SpriteEntry se = UGUIAtlasMaker.DuplicateSprite(mAtlas, sprite.name);
//						if (se != null) UGUISettings.selectedSprite = se.name;
//					}

//					if (GUILayout.Button("Save As..."))
//					{
//						#if UNITY_3_5
//						string path = EditorUtility.SaveFilePanel("Save As",
//						UGUISettings.currentPath, sprite.name + ".png", "png");
//						#else
//						string path = EditorUtility.SaveFilePanelInProject("Save As",
//							sprite.name + ".png", "png",
//							"Extract sprite into which file?", UGUISettings.currentPath);
//						#endif
//
//						if (!string.IsNullOrEmpty(path))
//						{
//							UGUISettings.currentPath = System.IO.Path.GetDirectoryName(path);
//							UGUIAtlasMaker.SpriteEntry se = UGUIAtlasMaker.ExtractSprite(mAtlas, sprite.name);
//
//							if (se != null)
//							{
//								byte[] bytes = se.tex.EncodeToPNG();
//								File.WriteAllBytes(path, bytes);
//								AssetDatabase.ImportAsset(path);
//								if (se.temporaryTexture) DestroyImmediate(se.tex);
//							}
//						}
//					}
                    GUILayout.EndHorizontal();
                    UGUIEditorTools.EndContents();
                }

                if (UGUIEditorTools.DrawHeader("Modify"))
                {
                    UGUIEditorTools.BeginContents();

                    EditorGUILayout.BeginHorizontal();
                    GUILayout.Space(20f);
                    EditorGUILayout.BeginVertical();

                    UGUISettings.backgroundColor = EditorGUILayout.ColorField("Background", UGUISettings.backgroundColor);

//					if (GUILayout.Button("Add a Shadow")) AddShadow(sprite);
//					if (GUILayout.Button("Add a Soft Outline")) AddOutline(sprite);

//					if (GUILayout.Button("Add a Transparent Border")) AddTransparentBorder(sprite);
//					if (GUILayout.Button("Add a Clamped Border")) AddClampedBorder(sprite);
//					if (GUILayout.Button("Add a Tiled Border")) AddTiledBorder(sprite);
//					EditorGUI.BeginDisabledGroup(!sprite.hasBorder);
//					if (GUILayout.Button("Crop Border")) CropBorder(sprite);
//					EditorGUI.EndDisabledGroup();

                    EditorGUILayout.EndVertical();
                    GUILayout.Space(20f);
                    EditorGUILayout.EndHorizontal();

                    UGUIEditorTools.EndContents();
                }

                if (UGUIEditorTools.previousSelection != null)
                {
                    GUILayout.Space(3f);
                    GUI.backgroundColor = Color.green;

                    if (GUILayout.Button("<< Return to " + UGUIEditorTools.previousSelection.name))
                    {
                        UGUIEditorTools.SelectPrevious();
                    }
                    GUI.backgroundColor = Color.white;
                }
            }
        }
    }
Ejemplo n.º 27
0
    public override void OnInspectorGUI()
    {
        serializedObject.Update();

        if (atlasProperty != null)
        {
            using (EditorGUILayout.HorizontalScope horizontalScope = new EditorGUILayout.HorizontalScope()) {
                if (GUILayout.Button("Atlas", "DropDown", GUILayout.Width(EditorGUIUtility.labelWidth - 10)) == true)
                {
                    AtlasSelector.Show(null, OnDoubleClickAtlas, atlasProperty.objectReferenceValue as UGUIAtlas);
                }
                //绘制图集属性
                UGUIAtlas selectedAtlas = EditorGUILayout.ObjectField(atlasProperty.objectReferenceValue, typeof(UGUIAtlas), true) as UGUIAtlas;
                if (selectedAtlas != atlasProperty.objectReferenceValue)
                {
                    atlasProperty.objectReferenceValue  = selectedAtlas;
                    spriteProperty.objectReferenceValue = null;
                    serializedObject.ApplyModifiedProperties();
                }
            }


            using (EditorGUILayout.HorizontalScope horizontalScope = new EditorGUILayout.HorizontalScope()) {
                var atlas = atlasProperty.objectReferenceValue as UGUIAtlas;
                if (atlas != null)
                {
                    if (GUILayout.Button("Sprite", "DropDown", GUILayout.Width(EditorGUIUtility.labelWidth - 10)) == true)
                    {
                        AtlasSpriteSelector.Show(atlas, OnClickSprite, OnDoubleClickSprite, spriteProperty.objectReferenceValue as Sprite);
                    }
                }
                else
                {
                    GUILayout.Label("Sprite", GUILayout.Width(EditorGUIUtility.labelWidth - 10));
                }

                using (EditorGUI.DisabledScope disableGroup = new EditorGUI.DisabledScope(atlas != null)) {
                    using (EditorGUI.ChangeCheckScope changeCheckScope = new EditorGUI.ChangeCheckScope()) {
                        EditorGUILayout.PropertyField(spriteProperty, new GUIContent(""));

                        if (changeCheckScope.changed == true)
                        {
                            var newSprite = spriteProperty.objectReferenceValue as Sprite;
                            if (newSprite)
                            {
                                Image.Type oldType = (Image.Type)typeProperty.enumValueIndex;
                                if (newSprite.border.SqrMagnitude() > 0)
                                {
                                    typeProperty.enumValueIndex = (int)Image.Type.Sliced;
                                }
                                else if (oldType == Image.Type.Sliced)
                                {
                                    typeProperty.enumValueIndex = (int)Image.Type.Simple;
                                }
                            }
                        }
                        serializedObject.ApplyModifiedProperties();
                    }
                }
            }

            AppearanceControlsGUI();
            RaycastControlsGUI();

            showType.target = spriteProperty.objectReferenceValue != null;
            if (EditorGUILayout.BeginFadeGroup(showType.faded))
            {
                TypeGUI();
            }
            EditorGUILayout.EndFadeGroup();

            SetShowNativeSize(true, true);
            if (EditorGUILayout.BeginFadeGroup(m_ShowNativeSize.faded))
            {
                EditorGUI.indentLevel++;
                EditorGUILayout.PropertyField(preserveAspectProperty);
                EditorGUI.indentLevel--;
            }
            EditorGUILayout.EndFadeGroup();

            NativeSizeButtonGUI();
            serializedObject.ApplyModifiedProperties();
        }
    }