Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions Editor/AssetProcessor/ShaderModifyListener.cs
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@ private static void OnPostprocessAllAssets(string[] importedAssets, string[] del
)
{
var shader = AssetDatabase.LoadAssetAtPath<Shader>(assetPath);
if (shader == null) continue;

MetaDataHelper.ReleaseShaderMetadataCache(shader);
ShaderPerfMonitor.ClearShaderPerfCache(shader);
ReflectionHelper.InvalidatePropertyCache(shader);
Expand Down
52 changes: 52 additions & 0 deletions Editor/Helper/IOHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,35 @@ public static string GetValidFileName(string text)

public static bool ExistAndNotEmpty(string filePath) => File.Exists(filePath) && new FileInfo(filePath).Length > 1;

public static string GenerateUniqueFileName(string directory, string baseName, string extension)
{
var absDirectory = GetAbsPath(directory);

string rootName = baseName;
int startIndex = 1;

int lastUnderscore = baseName.LastIndexOf('_');
if (lastUnderscore >= 0 && lastUnderscore < baseName.Length - 1
&& int.TryParse(baseName.Substring(lastUnderscore + 1), out int existingIndex))
{
rootName = baseName.Substring(0, lastUnderscore);
startIndex = existingIndex + 1;
}

if (!File.Exists(Path.Combine(absDirectory, baseName + "." + extension)))
return baseName;

int index = startIndex;
string candidate;
do
{
candidate = rootName + "_" + index;
index++;
} while (File.Exists(Path.Combine(absDirectory, candidate + "." + extension)));

return candidate;
}

public static void WriteBinaryFile(string filePath, byte[] bytes)
{
try
Expand Down Expand Up @@ -105,6 +134,26 @@ public static string ReadTextFile(string filePath)
}
}

/// <summary>
/// <para>Displays the "save file" dialog and returns the selected path name.</para>
/// </summary>
/// <param name="title">The title of the window to display.</param>
/// <param name="relativeDirectory">The working directory that this dialog opens on.</param>
/// <param name="defaultName">The placeholder text to display in the "Save As" text field. This is the name of file to be saved. </param>
/// <param name="extension">The file extension to use in the saved file path. For example, enter "png" to save an image in the PNG format.</param>
/// <returns>
/// <para>A string absolute path to the saved file if the dialog was canceled or the save failed, it returns an empty string.</para>
/// </returns>
public static string SaveFilePanel(string title, string relativeDirectory, string defaultName, string extension)
{
// When a new folder is created in the file selection window, the Current Work Directory is modified, which causes Unity to crash
var savedCwd = Directory.GetCurrentDirectory();
var absPath = EditorUtility.SaveFilePanel(title, relativeDirectory, defaultName, extension);
Directory.SetCurrentDirectory(savedCwd);

return absPath;
}

#endregion

#region Process
Expand Down Expand Up @@ -198,6 +247,9 @@ public static string GetCompiledShaderVariantCacheDirectory(Shader shader, Shade

public static void ClearShaderPerfCache(Shader shader)
{
if (shader == null)
return;

try
{
var shaderDir = GetCompiledShaderCacheRootDirectory(shader);
Expand Down
13 changes: 6 additions & 7 deletions Editor/Helper/MetaDataHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,7 @@ public static void ReleaseAllShadersMetadataCache()

public static void ReleaseShaderMetadataCache(Shader shader)
{
if (shader && _perShaderCachesDic.ContainsKey(shader))
if (shader)
_perShaderCachesDic.Remove(shader);
}

Expand All @@ -128,18 +128,17 @@ public static void ReleaseAllMaterialsMetadataCache(Shader shader)
public static void ReleaseMaterialMetadataCache(Material material)
{
if (material
&& material.shader
&& _perShaderCachesDic.ContainsKey(material.shader)
&& _perShaderCachesDic[material.shader].perMaterialDataCachesDic.ContainsKey(material))
&& material.shader
&& _perShaderCachesDic.ContainsKey(material.shader))
_perShaderCachesDic[material.shader].perMaterialDataCachesDic.Remove(material);
}

public static void ForceUpdateAllMaterialsMetadataCache(Shader shader)
{
if (shader && _perShaderCachesDic.ContainsKey(shader))
if (shader && _perShaderCachesDic.TryGetValue(shader, out var perShaderCache))
{
foreach (var perMaterialCachKWPair in _perShaderCachesDic[shader].perMaterialDataCachesDic)
perMaterialCachKWPair.Value.perMaterialData.forceInit = true;
foreach (var perMaterialCacheKWPair in perShaderCache.perMaterialDataCachesDic)
perMaterialCacheKWPair.Value.perMaterialData.forceInit = true;
}
}

Expand Down
1 change: 1 addition & 0 deletions Editor/Helper/ToolbarHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -267,6 +267,7 @@ private static bool FindMaterialAssetByMaterialInstance(Material material, LWGUI
materialAsset = onFindMaterialAssetInRendererByMaterialInstance(renderer, material);
}

// Look for renderer.sharedMaterials as a fallback, if the runtime has modified the sharedMaterials will not work
if (materialAsset == null)
{
int index = renderer.materials.ToList().FindIndex(materialInstance => materialInstance == material);
Expand Down
11 changes: 3 additions & 8 deletions Editor/ScriptableObject/LwguiRampAtlas.cs
Original file line number Diff line number Diff line change
Expand Up @@ -348,10 +348,8 @@ private void OnEnable()

private void OnValidate()
{
// Skip at the end of compilation
if (Event.current == null
// Skip when editing Text Field
|| EditorGUIUtility.editingTextField)
// It is only called when the user manually saves it, avoiding some strange bugs
if (!_saveTextureToggle)
return;

InitData();
Expand Down Expand Up @@ -504,10 +502,7 @@ public static LwguiRampAtlas SaveRampAtlasSOToAsset(LwguiRampAtlas rampAtlasSO,
string createdFileRelativePath = string.Empty;
while (true)
{
// TODO: Warning:
// PropertiesGUI() is being called recursively. If you want to render the default gui for shader properties then call PropertiesDefaultGUI() instead
var absPath = EditorUtility.SaveFilePanel("Create a Ramp Atlas SO", rootPath, defaultFileName, "asset");

var absPath = IOHelper.SaveFilePanel("Create a Ramp Atlas SO", rootPath, defaultFileName, "asset");
if (absPath.StartsWith(IOHelper.ProjectPath))
{
createdFileRelativePath = IOHelper.GetRelativePath(absPath);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,20 +9,20 @@ namespace LWGUI
/// Control whether the property can be edited based on multiple conditions.
///
/// logicalOperator: And | Or (Default: And).
/// propName: Target Property Name used for comparison.
/// propNameOrKeyword: Target Property Name or Keyword used for comparison. If no matching property is found, it falls back to checking material keywords (enabled = 1, disabled = 0).
/// compareFunction: Less (L) | Equal (E) | LessEqual (LEqual / LE) | Greater (G) | NotEqual (NEqual / NE) | GreaterEqual (GEqual / GE).
/// value: Target Property Value used for comparison.
/// </summary>
public class ActiveIfDecorator : SubDrawer
{
public ShowIfDecorator.ShowIfData activeIfData = new();

public ActiveIfDecorator(string propName, string comparisonMethod, float value) : this("And", propName, comparisonMethod, value) { }
public ActiveIfDecorator(string propNameOrKeyword, string comparisonMethod, float value) : this("And", propNameOrKeyword, comparisonMethod, value) { }

public ActiveIfDecorator(string logicalOperator, string propName, string compareFunction, float value)
public ActiveIfDecorator(string logicalOperator, string propNameOrKeyword, string compareFunction, float value)
{
activeIfData.logicalOperator = logicalOperator.ToLower() == "or" ? ShowIfDecorator.LogicalOperator.Or : ShowIfDecorator.LogicalOperator.And;
activeIfData.targetPropertyName = propName;
activeIfData.targetPropertyNameOrKeyword = propNameOrKeyword;
activeIfData.compareFunction = ShowIfDecorator.ParseCompareFunction(compareFunction);
activeIfData.value = value;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ namespace LWGUI
/// Control the show or hide of a single or a group of properties based on multiple conditions.
///
/// logicalOperator: And | Or (Default: And).
/// propName: Target Property Name used for comparison.
/// propNameOrKeyword: Target Property Name or Keyword used for comparison. If no matching property is found, it falls back to checking material keywords (enabled = 1, disabled = 0).
/// compareFunction: Less (L) | Equal (E) | LessEqual (LEqual / LE) | Greater (G) | NotEqual (NEqual / NE) | GreaterEqual (GEqual / GE).
/// value: Target Property Value used for comparison.
/// </summary>
Expand All @@ -27,10 +27,10 @@ public enum LogicalOperator

public class ShowIfData
{
public LogicalOperator logicalOperator = LogicalOperator.And;
public string targetPropertyName = string.Empty;
public CompareFunction compareFunction = CompareFunction.Equal;
public float value = 0;
public LogicalOperator logicalOperator = LogicalOperator.And;
public string targetPropertyNameOrKeyword = string.Empty;
public CompareFunction compareFunction = CompareFunction.Equal;
public float value = 0;
}

public ShowIfData showIfData = new();
Expand Down Expand Up @@ -68,12 +68,12 @@ public static CompareFunction ParseCompareFunction(string compareFunction)
return (CompareFunction)Enum.Parse(typeof(CompareFunction), compareFunctionName);
}

public ShowIfDecorator(string propName, string comparisonMethod, float value) : this("And", propName, comparisonMethod, value) { }
public ShowIfDecorator(string propNameOrKeyword, string comparisonMethod, float value) : this("And", propNameOrKeyword, comparisonMethod, value) { }

public ShowIfDecorator(string logicalOperator, string propName, string compareFunction, float value)
public ShowIfDecorator(string logicalOperator, string propNameOrKeyword, string compareFunction, float value)
{
showIfData.logicalOperator = logicalOperator.ToLower() == "or" ? LogicalOperator.Or : LogicalOperator.And;
showIfData.targetPropertyName = propName;
showIfData.targetPropertyNameOrKeyword = propNameOrKeyword;
showIfData.compareFunction = ParseCompareFunction(compareFunction);
showIfData.value = value;
}
Expand Down Expand Up @@ -137,12 +137,19 @@ public static bool GetShowIfResultToFilterDrawerApplying(MaterialProperty prop)
return GetShowIfResultFromMaterial(showIfDatas, material);
}

public static float GetTargetValue(ShowIfData showIfData, Material material)
{
if (material.HasProperty(showIfData.targetPropertyNameOrKeyword))
return material.GetFloat(showIfData.targetPropertyNameOrKeyword);
return material.IsKeywordEnabled(showIfData.targetPropertyNameOrKeyword) ? 1f : 0f;
}

public static bool GetShowIfResultFromMaterial(List<ShowIfData> showIfDatas, Material material)
{
bool result = true;
foreach (var showIfData in showIfDatas)
{
var targetValue = material.GetFloat(showIfData.targetPropertyName);
var targetValue = GetTargetValue(showIfData, material);
Compare(showIfData, targetValue, ref result);
}

Expand All @@ -153,7 +160,11 @@ public static void GetShowIfResult(PropertyStaticData propStaticData, PropertyDy
{
foreach (var showIfData in propStaticData.showIfDatas)
{
var targetValue = perMaterialData.propDynamicDatas[showIfData.targetPropertyName].property.floatValue;
float targetValue;
if (perMaterialData.propDynamicDatas.TryGetValue(showIfData.targetPropertyNameOrKeyword, out var targetPropDynamicData))
targetValue = targetPropDynamicData.property.floatValue;
else
targetValue = perMaterialData.material.IsKeywordEnabled(showIfData.targetPropertyNameOrKeyword) ? 1f : 0f;
Compare(showIfData, targetValue, ref propDynamicData.isShowing);
}
}
Expand Down

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading