using System;
using System.Collections;
using System.Collections.Generic;
using PWCommon4;
using UnityEditor;
using UnityEngine;
using UnityEditor.SceneManagement;
using System.Linq;
namespace Gaia
{
public static class GaiaEditorUtils
{
///
/// Marks the active scene dirty.
///
public static void MarkSceneDirty()
{
if (!Application.isPlaying)
{
if (!EditorSceneManager.GetActiveScene().isDirty)
{
EditorSceneManager.MarkSceneDirty(EditorSceneManager.GetActiveScene());
}
}
}
///
/// Helper function to create a GUIContent for button icons - decided whether to use Unity Standard or Pro Icons and adds the tooltip text
///
/// Localization key to get the tooltip from
/// Icon to use for Unity Standard Skin
/// Icon to use for Unity Pro Skin
/// Current Editor Utils class to pass in to look up the localized tooltip
/// GUIContent with correct texture and tooltip.
public static GUIContent GetIconGUIContent(string key, Texture2D standardIcon, Texture2D proIcon, EditorUtils editorUtils)
{
Texture icon = null;
if (EditorGUIUtility.isProSkin)
{
icon = proIcon;
}
else
{
icon = standardIcon;
}
return new GUIContent(icon, editorUtils.GetTooltip(key));
}
///
/// Displays the resource helper window with the given operation
///
/// The operation mode to open the window with.
public static void ShowResourceHelperWindow(GaiaResourceHelperOperation operation, Vector2 position)
{
var resourceHelper = EditorWindow.GetWindow(false, "Resource Helper");
resourceHelper.Show();
resourceHelper.position = new Rect(position, new Vector2(300, 200));
resourceHelper.minSize = new Vector2(300, 200);
resourceHelper.m_operation = operation;
}
///
/// Draws a line that can be used on curves and gradients to show where the current time of day is.
///
///
///
///
public static void DrawTimeOfDayLine(float currentTime, float lineHeight = 0f)
{
Rect currentRect = GUILayoutUtility.GetLastRect();
EditorGUI.DrawRect(new Rect(currentRect.x + EditorGUIUtility.labelWidth + Mathf.Lerp(0f, currentRect.width - EditorGUIUtility.labelWidth, currentTime), currentRect.y - EditorGUIUtility.singleLineHeight * lineHeight, 2f, EditorGUIUtility.singleLineHeight * (lineHeight + 1)), Color.red);
}
///
/// Used to draw the line on the kelvin gradient image.
///
///
///
///
public static void DrawKelvinLine(float currentTime, float heightValue = 0f, float lineHeight = 0f)
{
Rect currentRect = GUILayoutUtility.GetLastRect();
EditorGUI.DrawRect(new Rect(currentRect.x + EditorGUIUtility.labelWidth + Mathf.Lerp(3.5f, currentRect.width - EditorGUIUtility.labelWidth - 3.5f, currentTime), currentRect.y - EditorGUIUtility.singleLineHeight * lineHeight, 2f, EditorGUIUtility.singleLineHeight * (lineHeight + 1) + heightValue), Color.green);
}
///
/// Allows to loads an asset from unitys hidden builtin-extra resources
///
///
///
///
public static T LoadAssetFromUniqueAssetPath(string aAssetPath) where T : UnityEngine.Object
{
if (aAssetPath.Contains("::"))
{
string[] parts = aAssetPath.Split(new string[] { "::" }, System.StringSplitOptions.RemoveEmptyEntries);
aAssetPath = parts[0];
if (parts.Length > 1)
{
string assetName = parts[1];
System.Type t = typeof(T);
var assets = AssetDatabase.LoadAllAssetsAtPath(aAssetPath)
.Where(i => t.IsAssignableFrom(i.GetType())).Cast();
var obj = assets.Where(i => i.name == assetName).FirstOrDefault();
if (obj == null)
{
int id;
if (int.TryParse(parts[1], out id))
obj = assets.Where(i => i.GetInstanceID() == id).FirstOrDefault();
}
if (obj != null)
return obj;
}
}
return AssetDatabase.LoadAssetAtPath(aAssetPath);
}
public static string GetUniqueAssetPath(UnityEngine.Object aObj)
{
string path = AssetDatabase.GetAssetPath(aObj);
if (!string.IsNullOrEmpty(aObj.name))
path += "::" + aObj.name;
else
path += "::" + aObj.GetInstanceID();
return path;
}
///
/// Handy layer mask interface - creates a EditorGUILayout Mask field
///
///
///
///
public static LayerMask LayerMaskField(GUIContent label, LayerMask layerMask)
{
List layers = new List();
List layerNumbers = new List();
for (int i = 0; i < 32; i++)
{
string layerName = LayerMask.LayerToName(i);
if (layerName != "")
{
layers.Add(layerName);
layerNumbers.Add(i);
}
}
int maskWithoutEmpty = 0;
for (int i = 0; i < layerNumbers.Count; i++)
{
if (((1 << layerNumbers[i]) & layerMask.value) > 0)
maskWithoutEmpty |= (1 << i);
}
maskWithoutEmpty = EditorGUILayout.MaskField(label, maskWithoutEmpty, layers.ToArray());
int mask = 0;
for (int i = 0; i < layerNumbers.Count; i++)
{
if ((maskWithoutEmpty & (1 << i)) > 0)
mask |= (1 << layerNumbers[i]);
}
layerMask.value = mask;
return layerMask;
}
///
/// Handy layer mask interface - creates a EditorGUI Mask field
///
///
///
///
public static LayerMask LayerMaskFieldRect(Rect rect, GUIContent label, LayerMask layerMask)
{
List layers = new List();
List layerNumbers = new List();
for (int i = 0; i < 32; i++)
{
string layerName = LayerMask.LayerToName(i);
if (layerName != "")
{
layers.Add(layerName);
layerNumbers.Add(i);
}
}
int maskWithoutEmpty = 0;
for (int i = 0; i < layerNumbers.Count; i++)
{
if (((1 << layerNumbers[i]) & layerMask.value) > 0)
maskWithoutEmpty |= (1 << i);
}
maskWithoutEmpty = EditorGUI.MaskField(rect, label, maskWithoutEmpty, layers.ToArray());
int mask = 0;
for (int i = 0; i < layerNumbers.Count; i++)
{
if ((maskWithoutEmpty & (1 << i)) > 0)
mask |= (1 << layerNumbers[i]);
}
layerMask.value = mask;
return layerMask;
}
}
}