Example #1
0
 /// <summary>
 /// This method is called when the a preview is created.
 /// </summary>
 public static void PreviewCreated(PartBehaviour part)
 {
     if (OnPreviewCreated != null)
     {
         OnPreviewCreated.Invoke(part);
     }
 }
        private void OnGUI()
        {
            if (targetObject != null)
            {
                if (previewEditor == null)
                {
                    previewEditor             = Editor.CreateEditor(targetObject, typeof(CustomPreviewEditor)) as CustomPreviewEditor;
                    previewEditor.TargetAsset = targetObject;
                    OnPreviewCreated?.Invoke(previewEditor);
                }
                if (previewEditor.HasPreviewGUI())
                {
                    previewEditor.OnInteractivePreviewGUI(new Rect(0, 0, previewResolutions[previewResIndex], previewResolutions[previewResIndex]), null);
                }
            }

            GUILayout.BeginArea(new Rect(previewResolutions[previewResIndex], 0, 200, 200));

            GUILayout.BeginVertical();

            GUILayout.Label("Gameobject to create icon from");

            EditorGUI.BeginChangeCheck();
            targetObject = EditorGUILayout.ObjectField(targetObject, typeof(GameObject), false) as GameObject;
            if (EditorGUI.EndChangeCheck())
            {
                DestroyImmediate(previewEditor);
                if (targetObject != null)
                {
                    iconName = targetObject.name;
                }
            }

            GUILayout.BeginHorizontal();
            GUILayout.Label("Icon Resolution");

            EditorGUI.BeginChangeCheck();
            previewResIndex = EditorGUILayout.Popup(previewResIndex, Array.ConvertAll <int, string>(previewResolutions, x => x.ToString()));
            if (EditorGUI.EndChangeCheck())
            {
                GUI.changed = true;
            }
            GUILayout.EndHorizontal();

            if (targetObject != null)
            {
                EditorGUI.BeginChangeCheck();
                isTransparent = GUILayout.Toggle(isTransparent, "isTransparent", GUI.skin.button);
                if (EditorGUI.EndChangeCheck())
                {
                    if (isTransparent)
                    {
                        previewEditor.SetBackgroundColor(Color.magenta, true);
                    }
                    else
                    {
                        previewEditor.SetBackgroundColor(iconBackgroundColor);
                    }
                }

                EditorGUI.BeginChangeCheck();
                bgTexture = EditorGUILayout.ObjectField(bgTexture, typeof(Texture2D), false) as Texture2D;
                if (EditorGUI.EndChangeCheck())
                {
                    if (bgTexture != null)
                    {
                        previewEditor.BGTexture = bgTexture;
                    }
                    else
                    {
                        previewEditor.BGTexture = null;
                    }
                }


                if (!isTransparent)
                {
                    GUILayout.BeginHorizontal();
                    GUILayout.Label("Background Color");
                    EditorGUI.BeginChangeCheck();
                    iconBackgroundColor = EditorGUILayout.ColorField(iconBackgroundColor);
                    if (EditorGUI.EndChangeCheck())
                    {
                        previewEditor.SetBackgroundColor(iconBackgroundColor);
                    }
                    GUILayout.EndHorizontal();
                }

                GUILayout.BeginHorizontal();
                GUILayout.Label("Icon name");
                iconName = GUILayout.TextField(iconName);
                GUILayout.EndHorizontal();

                if (GUILayout.Button("Create Icon"))
                {
                    if (previewEditor.PreviewTexture != null)
                    {
                        CreatePngFromTexture();
                    }
                }
            }

            GUILayout.EndVertical();

            GUILayout.EndArea();
        }
        private void OnGUI()
        {
            _customStyle         = new GUIStyle(GUI.skin.button);
            _customStyle.padding = new RectOffset(10, 10, 10, 10);

            _titleStyle           = new GUIStyle(GUI.skin.button);
            _titleStyle.fontStyle = FontStyle.Bold;
            _titleStyle.alignment = TextAnchor.MiddleCenter;

            if (_targetObject != null)
            {
                if (_previewWindow == null)
                {
                    _previewWindow             = Editor.CreateEditor(_targetObject, typeof(CustomPreviewEditor)) as CustomPreviewEditor;
                    _previewWindow.TargetAsset = _targetObject;
                    OnPreviewCreated?.Invoke(_previewWindow);
                    UpdateOptionsToPreviewSettings();
                }
                if (_previewWindow.HasPreviewGUI())
                {
                    _previewWindow.OnInteractivePreviewGUI(_previewRect, null);
                    UpdateOptionsToPreviewSettings();
                }
            }
            else
            {
                GUILayout.BeginArea(_previewRect);
                GUI.Box(new Rect(0, 0, _previewRect.width, _previewRect.height), "Image Not Available", _customStyle);
                GUILayout.EndArea();
            }

            /*
             * Icon Options Area
             */
            GUILayout.BeginArea(_iconOptionsArea, _customStyle);

            GUILayout.BeginVertical();

            GUILayout.Label("Icon Options", _titleStyle);

            if (_targetObject != null)
            {
                GUILayout.BeginHorizontal();
                GUILayout.Label("Transparency");
                EditorGUI.BeginChangeCheck();
                _isTransparent = GUILayout.Toggle(_isTransparent, _isTransparent.ToString() /*, GUI.skin.button*/);
                if (EditorGUI.EndChangeCheck())
                {
                    if (_isTransparent)
                    {
                        _previewWindow.SetBackgroundColor(_transparencyColor, true);
                    }
                    else
                    {
                        _previewWindow.SetBackgroundColor(_iconBGColor);
                    }
                }
                GUILayout.EndHorizontal();

                if (!_isTransparent)
                {
                    GUILayout.BeginHorizontal();
                    GUILayout.Label("Background Color");
                    EditorGUI.BeginChangeCheck();
                    _iconBGColor = EditorGUILayout.ColorField(_iconBGColor);
                    if (EditorGUI.EndChangeCheck())
                    {
                        _previewWindow.SetBackgroundColor(_iconBGColor);
                    }
                    GUILayout.EndHorizontal();
                }

                GUILayout.BeginHorizontal();
                GUILayout.Label("Background Texture");
                EditorGUI.BeginChangeCheck();
                _bgTexture = EditorGUILayout.ObjectField(_bgTexture, typeof(Texture2D), false) as Texture2D;
                if (EditorGUI.EndChangeCheck())
                {
                    if (_bgTexture != null)
                    {
                        _previewWindow.BGTexture = _bgTexture;
                    }
                    else
                    {
                        _previewWindow.BGTexture = null;
                    }
                }
                GUILayout.EndHorizontal();

                GUILayout.BeginHorizontal();
                GUILayout.Label("Foreground Texture");
                EditorGUI.BeginChangeCheck();
                _fgTexture = EditorGUILayout.ObjectField(_fgTexture, typeof(Texture2D), false) as Texture2D;
                if (EditorGUI.EndChangeCheck())
                {
                    if (_fgTexture != null)
                    {
                        _previewWindow.FGTExture = _fgTexture;
                    }
                    else
                    {
                        _previewWindow.FGTExture = null;
                    }
                }
                GUILayout.EndHorizontal();

                if (GUILayout.Button("Reset Icon Options"))
                {
                    ResetIconOptions();
                }
            }

            GUILayout.EndVertical();
            GUILayout.EndArea();

            /*
             * Below Preview Options
             */
            GUILayout.BeginArea(_previewOptionsArea, _customStyle);

            GUILayout.BeginVertical();

            GUILayout.Label("Preview Window Options", _titleStyle);

            GUILayout.BeginHorizontal();
            GUILayout.Label("Icon Object");
            EditorGUI.BeginChangeCheck();
            _targetObject = EditorGUILayout.ObjectField(_targetObject, typeof(GameObject), false) as GameObject;
            if (EditorGUI.EndChangeCheck())
            {
                DestroyImmediate(_previewWindow);
                if (_targetObject != null)
                {
                    _iconName = _targetObject.name;
                }
            }
            GUILayout.EndHorizontal();

            if (_targetObject != null)
            {
                GUILayout.BeginHorizontal();
                GUILayout.Label("Light One Color");
                EditorGUI.BeginChangeCheck();
                _previewLightOneColor = EditorGUILayout.ColorField(_previewLightOneColor);
                if (EditorGUI.EndChangeCheck())
                {
                    _previewWindow.LightOneColor = _previewLightOneColor;
                }
                GUILayout.EndHorizontal();

                GUILayout.BeginHorizontal();
                GUILayout.Label("Light One Intensity");
                EditorGUI.BeginChangeCheck();
                _previewLightOneIntensity = EditorGUILayout.Slider(_previewLightOneIntensity, 0, 2.0f);
                if (EditorGUI.EndChangeCheck())
                {
                    _previewWindow.LightOneIntensity = _previewLightOneIntensity;
                }
                GUILayout.EndHorizontal();

                GUILayout.BeginHorizontal();
                GUILayout.Label("Light Two Color");
                EditorGUI.BeginChangeCheck();
                _previewLightTwoColor = EditorGUILayout.ColorField(_previewLightTwoColor);
                if (EditorGUI.EndChangeCheck())
                {
                    _previewWindow.LightTwoColor = _previewLightTwoColor;
                }
                GUILayout.EndHorizontal();

                GUILayout.BeginHorizontal();
                GUILayout.Label("Light Two Intensity");
                EditorGUI.BeginChangeCheck();
                _previewLightTwoIntensity = EditorGUILayout.Slider(_previewLightTwoIntensity, 0, 2.0f);
                if (EditorGUI.EndChangeCheck())
                {
                    _previewWindow.LightTwoIntensity = _previewLightTwoIntensity;
                }
                GUILayout.EndHorizontal();

                EditorGUILayout.BeginHorizontal();
                if (GUILayout.Button("Reset Lights"))
                {
                    ResetWindowOptions();
                }

                if (GUILayout.Button("Reset Object"))
                {
                    ResetTarget();
                }
                EditorGUILayout.EndHorizontal();
            }

            GUILayout.EndVertical();
            GUILayout.EndArea();

            /*
             * Save Icon Options
             */

            GUILayout.BeginArea(_saveOptionsArea, _customStyle);

            GUILayout.BeginVertical();

            GUILayout.Label("Save Options", _titleStyle);

            if (_targetObject != null)
            {
                GUILayout.BeginHorizontal();
                GUILayout.Label("Icon Resolution");

                EditorGUI.BeginChangeCheck();
                _iconResIndex = EditorGUILayout.Popup(_iconResIndex, Array.ConvertAll <int, string>(_iconResolutions, x => x.ToString()));
                if (EditorGUI.EndChangeCheck())
                {
                    GUI.changed = true;
                }
                GUILayout.Label("x");
                GUILayout.Label(_iconResolutions[_iconResIndex].ToString());
                GUILayout.EndHorizontal();

                GUILayout.BeginHorizontal();
                GUILayout.Label("Icon Name");
                _iconName = GUILayout.TextField(_iconName);
                GUILayout.EndHorizontal();

                GUILayout.BeginHorizontal();
                GUILayout.Label("Icon Path:");
                _savePath = GUILayout.TextArea(_savePath);
                GUILayout.EndHorizontal();

                if (GUILayout.Button("Create Icon"))
                {
                    if (_previewWindow.PreviewTexture != null)
                    {
                        CreatePngFromTexture();
                    }
                }
            }
            GUILayout.EndVertical();
            GUILayout.EndArea();
        }