feat: add asset update system from v4 to v5

asset_update_system
mob-sakai 2024-05-23 00:09:13 +09:00
parent 66c76fb92b
commit 36ab069eba
32 changed files with 778 additions and 0 deletions

View File

@ -0,0 +1,3 @@
fileFormatVersion: 2
guid: 1006234332be4f329fc1830319b31aaa
timeCreated: 1704502465

View File

@ -0,0 +1,31 @@
using Coffee.UIExtensions;
using Coffee.UIParticleInternal.AssetModification;
using UnityEditor;
#pragma warning disable CS0612 // Type or member is obsolete
namespace Coffee.UIParticleInternal
{
internal class UIParticleComponentModifier_AbsoluteMode : ComponentModifier<UIParticle>
{
protected override bool ModifyComponent(UIParticle uip, bool dryRun)
{
if (!uip.m_AbsoluteMode) return false;
uip.m_AbsoluteMode = false;
uip.positionMode = UIParticle.PositionMode.Absolute;
if (!dryRun)
{
EditorUtility.SetDirty(uip);
}
return true;
}
public override string Report()
{
return " -> UIParticle.absoluteMode is obsolete. Use UIParticle.positionMode instead.\n";
}
}
}

View File

@ -0,0 +1,11 @@
fileFormatVersion: 2
guid: d3378b5e701274218b04cb5588b8a3bd
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:

View File

@ -0,0 +1,36 @@
using Coffee.UIExtensions;
using Coffee.UIParticleInternal.AssetModification;
using UnityEditor;
using UnityEngine;
#pragma warning disable CS0612 // Type or member is obsolete
namespace Coffee.UIParticleInternal
{
internal class UIParticleComponentModifier_AutoScaling : ComponentModifier<UIParticle>
{
protected override bool ModifyComponent(UIParticle uip, bool dryRun)
{
if (!uip.m_AutoScaling) return false;
uip.m_AutoScaling = false;
uip.autoScalingMode = UIParticle.AutoScalingMode.Transform;
uip.transform.localScale = Vector3.one;
if (!dryRun)
{
EditorUtility.SetDirty(uip);
EditorUtility.SetDirty(uip.transform);
}
return true;
}
public override string Report()
{
return
" -> UIParticle.ignoreCanvasScaler and UIParticle.autoScaling are obsolete." +
" Use UIParticle.autoScalingMode instead.\n";
}
}
}

View File

@ -0,0 +1,11 @@
fileFormatVersion: 2
guid: 3d6d0ca7ae8c641aa98b66fd91c05264
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:

View File

@ -0,0 +1,31 @@
using Coffee.UIExtensions;
using Coffee.UIParticleInternal.AssetModification;
using UnityEditor;
using UnityEngine;
#pragma warning disable CS0612 // Type or member is obsolete
namespace Coffee.UIParticleInternal
{
internal class UIParticleComponentModifier_IsTrail : ComponentModifier<UIParticle>
{
protected override bool ModifyComponent(UIParticle uip, bool dryRun)
{
if (!uip.m_IsTrail) return false;
if (!dryRun)
{
var go = uip.gameObject;
Object.DestroyImmediate(uip);
EditorUtility.SetDirty(go);
}
return true;
}
public override string Report()
{
return " -> UIParticle for trail is no longer needed. Removed.";
}
}
}

View File

@ -0,0 +1,11 @@
fileFormatVersion: 2
guid: dfbc9e244a2a040179e7f5b58ec0b978
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:

View File

@ -0,0 +1,45 @@
using System;
using System.Collections.Generic;
using Coffee.UIParticleInternal.AssetModification;
using UnityEditor;
namespace Coffee.UIParticleInternal
{
internal class UIParticleModifierRunner : Runner
{
public UIParticleModifierRunner()
: base("UIParticle v5", new List<(string, Func<string, Modifier>)>
{
(".unity", x => new SceneModifier
{
path = x,
componentModifiers = new IComponentModifier[]
{
new UIParticleRendererComponentModifier(),
new UIParticleComponentModifier_AutoScaling(),
new UIParticleComponentModifier_AbsoluteMode(),
new UIParticleComponentModifier_IsTrail()
}
}),
(".prefab", x => new PrefabModifier
{
path = x,
componentModifiers = new IComponentModifier[]
{
new UIParticleRendererComponentModifier(),
new UIParticleComponentModifier_AutoScaling(),
new UIParticleComponentModifier_AbsoluteMode(),
new UIParticleComponentModifier_IsTrail()
}
})
})
{
}
[MenuItem("UIParticleModifierRunner/Run")]
private static void Run()
{
new UIParticleModifierRunner().RunIfUserWantsTo();
}
}
}

View File

@ -0,0 +1,3 @@
fileFormatVersion: 2
guid: edd678db452e49869caeca7e7d269e5d
timeCreated: 1704502476

View File

@ -0,0 +1,29 @@
using Coffee.UIExtensions;
using Coffee.UIParticleInternal.AssetModification;
using UnityEditor;
using UnityEngine;
namespace Coffee.UIParticleInternal
{
internal class UIParticleRendererComponentModifier : ComponentModifier<UIParticleRenderer>
{
protected override bool ModifyComponent(UIParticleRenderer c, bool dryRun)
{
if (c.hideFlags.HasFlag(HideFlags.DontSave | HideFlags.NotEditable)) return false;
if (!dryRun)
{
var go = c.gameObject;
Object.DestroyImmediate(c);
EditorUtility.SetDirty(go);
}
return true;
}
public override string Report()
{
return " -> UIParticleRenderer component is now auto-generated object. Remove them.\n";
}
}
}

View File

@ -0,0 +1,11 @@
fileFormatVersion: 2
guid: b2a025408e4a7486a941102775d2c73b
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:

View File

@ -0,0 +1,8 @@
fileFormatVersion: 2
guid: 269bcefd175184eebbfa31421171fadf
folderAsset: yes
DefaultImporter:
externalObjects: {}
userData:
assetBundleName:
assetBundleVariant:

View File

@ -0,0 +1,33 @@
using System.Collections.Generic;
using UnityEditor;
using UnityEngine;
namespace Coffee.UIParticleInternal.AssetModification
{
internal abstract class ComponentModifier<T> : IComponentModifier where T : Component
{
private static readonly List<T> s_Components = new List<T>();
public bool isModified { get; private set; }
public bool ModifyComponent(GameObject root, bool dryRun)
{
root.GetComponentsInChildren(true, s_Components);
foreach (var c in s_Components)
{
if (PrefabUtility.IsPartOfAnyPrefab(c.gameObject)) continue;
if (ModifyComponent(c, dryRun))
{
isModified = true;
}
}
return isModified;
}
public abstract string Report();
protected abstract bool ModifyComponent(T component, bool dryRun);
}
}

View File

@ -0,0 +1,11 @@
fileFormatVersion: 2
guid: 338da2d8cec784add924489fc4a7bb01
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:

View File

@ -0,0 +1,43 @@
using System.Linq;
using System.Text;
using UnityEngine;
namespace Coffee.UIParticleInternal.AssetModification
{
internal abstract class GameObjectModifier : Modifier
{
private static readonly StringBuilder s_ReportLog = new StringBuilder();
public IComponentModifier[] componentModifiers;
protected bool ModifyGameObject(GameObject root, bool dryRun)
{
foreach (var modifier in componentModifiers)
{
modifier.ModifyComponent(root, dryRun);
}
return componentModifiers.Any(x => x.isModified);
}
protected override string ModificationReport()
{
if (!hasUpgraded) return string.Empty;
s_ReportLog.Length = 0;
foreach (var componentModifier in componentModifiers)
{
if (componentModifier.isModified)
{
s_ReportLog.Append(componentModifier.Report());
}
}
if (0 < s_ReportLog.Length)
{
s_ReportLog.Length--;
}
return s_ReportLog.ToString();
}
}
}

View File

@ -0,0 +1,11 @@
fileFormatVersion: 2
guid: f16bc447ca4934ca3b9329d1d870440a
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:

View File

@ -0,0 +1,11 @@
using UnityEngine;
namespace Coffee.UIParticleInternal.AssetModification
{
internal interface IComponentModifier
{
bool isModified { get; }
bool ModifyComponent(GameObject root, bool dryRun);
string Report();
}
}

View File

@ -0,0 +1,11 @@
fileFormatVersion: 2
guid: 1e50323d0799f4daf8ef476f7bf403a4
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:

View File

@ -0,0 +1,9 @@
using System.Text;
namespace Coffee.UIParticleInternal.AssetModification
{
internal interface ITextModifier
{
bool ModifyText(StringBuilder sb, string text);
}
}

View File

@ -0,0 +1,11 @@
fileFormatVersion: 2
guid: 2ad967b4f039b4deba09ed0c1401cf83
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:

View File

@ -0,0 +1,41 @@
using System;
namespace Coffee.UIParticleInternal.AssetModification
{
internal abstract class Modifier
{
private string _error;
public string path;
protected abstract string id { get; }
protected bool hasUpgraded { private set; get; }
public void Modify(bool dryRun)
{
try
{
hasUpgraded = RunModify(dryRun);
}
catch (Exception e)
{
_error = e.Message;
}
}
public string GetModificationReport()
{
return !string.IsNullOrEmpty(_error)
? $"<b><color=red>[{id} (Error)]</color> {path}</b> {_error}\n"
: hasUpgraded
? $"<b><color=green>[{id}]</color> {path}</b> {ModificationReport()}\n"
: string.Empty;
}
protected abstract bool RunModify(bool dryRun);
protected virtual string ModificationReport()
{
return string.Empty;
}
}
}

View File

@ -0,0 +1,11 @@
fileFormatVersion: 2
guid: c716fe53d75054d309923c577f5059a4
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:

View File

@ -0,0 +1,48 @@
using System;
using UnityEditor;
using UnityEngine;
namespace Coffee.UIParticleInternal.AssetModification
{
internal class PrefabModifier : GameObjectModifier
{
protected override string id => "Prefab";
protected override bool RunModify(bool dryRun)
{
using (var scope = new EditScope(path))
{
var changed = ModifyGameObject(scope.root, dryRun);
if (!dryRun && changed)
{
scope.Save();
}
return changed;
}
}
private readonly struct EditScope : IDisposable
{
private readonly string _path;
public readonly GameObject root;
public EditScope(string path)
{
_path = path;
root = PrefabUtility.LoadPrefabContents(path);
}
public void Dispose()
{
PrefabUtility.UnloadPrefabContents(root);
}
public void Save()
{
PrefabUtility.SaveAsPrefabAsset(root, _path);
}
}
}
}

View File

@ -0,0 +1,11 @@
fileFormatVersion: 2
guid: a50643d4fde75458cae1f0d4ef9549ad
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:

View File

@ -0,0 +1,97 @@
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using UnityEditor;
using UnityEditor.SceneManagement;
using UnityEngine;
namespace Coffee.UIParticleInternal.AssetModification
{
internal class Runner
{
private readonly List<(string ext, Func<string, Modifier> create)> _factory;
private readonly string _name;
protected Runner(string name, List<(string ext, Func<string, Modifier> create)> factory)
{
_name = name;
_factory = factory;
}
private Modifier CreateFromPath(string assetPath)
{
var ext = Path.GetExtension(assetPath);
return _factory
.FirstOrDefault(x => x.ext == ext)
.create?.Invoke(assetPath);
}
private Modifier[] GetModifiers(string[] assetPaths)
{
return assetPaths
.Where(x => x.StartsWith("Assets/", StringComparison.Ordinal))
.Select(CreateFromPath)
.Where(x => x != null)
.OrderBy(x => Path.GetExtension(x.path))
.ToArray();
}
public void RunIfUserWantsTo()
{
var select = EditorUtility.DisplayDialogComplex($"Upgrade {_name}",
"Upgrade all assets in this project?\n\n" +
"'Go Ahead': Upgrades all assets in this project using the old APIs. You should make a backup before proceeding.\n\n" +
"'Dry Run': Outputs the upgrade summary to the console without changing.", "I Made a Backup. Go Ahead!",
"No Thanks", "Dry Run");
if (select == 1) return;
if (!EditorSceneManager.SaveCurrentModifiedScenesIfUserWantsTo()) return;
var assetPaths = AssetDatabase.GetAllAssetPaths();
var dryRun = select == 2;
Run(assetPaths, dryRun);
}
public void Run(string[] assetPaths, bool dryRun)
{
var modifiers = GetModifiers(assetPaths);
var canceled = false;
try
{
AssetDatabase.StartAssetEditing();
EditorSceneManager.NewScene(NewSceneSetup.EmptyScene, NewSceneMode.Single);
for (var i = 0; i < modifiers.Length; i++)
{
var percentage = (float)i / modifiers.Length;
var m = modifiers[i];
if (EditorUtility.DisplayCancelableProgressBar("Upgrading...", m.path, percentage))
{
canceled = true;
break;
}
m.Modify(dryRun);
}
}
catch (Exception e)
{
Debug.LogException(e);
}
finally
{
var sb = new StringBuilder();
sb.Append(dryRun ? "<b>[DryRun]</b> " : "");
sb.AppendLine($"<b>Modify '{_name}' is {(canceled ? "canceled" : "completed")}.</b>");
sb.AppendLine("==== Modifications ====");
Debug.Log(modifiers.Aggregate(sb, (x, m) => x.Append(m.GetModificationReport())));
EditorUtility.ClearProgressBar();
AssetDatabase.StopAssetEditing();
AssetDatabase.Refresh();
}
}
}
}

View File

@ -0,0 +1,11 @@
fileFormatVersion: 2
guid: f28067cf4bfca4d92bd5262ac5c7a652
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:

View File

@ -0,0 +1,53 @@
using System;
using UnityEditor.SceneManagement;
using UnityEngine.SceneManagement;
namespace Coffee.UIParticleInternal.AssetModification
{
internal class SceneModifier : GameObjectModifier
{
protected override string id => "Scene";
protected override bool RunModify(bool dryRun)
{
using (var scope = new EditScope(path))
{
var changed = false;
foreach (var root in scope.scene.GetRootGameObjects())
{
if (ModifyGameObject(root, dryRun))
{
changed = true;
}
}
if (!dryRun && changed)
{
scope.Save();
}
return changed;
}
}
private readonly struct EditScope : IDisposable
{
public readonly Scene scene;
public EditScope(string path)
{
scene = EditorSceneManager.OpenScene(path, OpenSceneMode.Additive);
}
public void Dispose()
{
EditorSceneManager.CloseScene(scene, true);
}
public void Save()
{
EditorSceneManager.SaveScene(scene);
}
}
}
}

View File

@ -0,0 +1,11 @@
fileFormatVersion: 2
guid: 6764c0c6fd61a4f66a8b0e3467be420d
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:

View File

@ -0,0 +1,78 @@
using System;
using System.Collections.Generic;
using System.IO;
using System.Text;
namespace Coffee.UIParticleInternal.AssetModification
{
internal class TextAssetModifier : Modifier
{
public ITextModifier[] textModifiers;
protected override string id => "Text";
protected virtual string savePath => path;
protected override bool RunModify(bool dryRun)
{
var changed = false;
using (var scope = new EditScope(path, savePath))
{
foreach (var line in scope.lines)
{
if (ModifyLine(scope.sb, line))
{
changed = true;
}
else
{
scope.sb.AppendLine(line);
}
}
if (!dryRun && changed)
{
scope.Save();
}
return changed;
}
}
private bool ModifyLine(StringBuilder sb, string line)
{
foreach (var modifier in textModifiers)
{
if (modifier.ModifyText(sb, line))
{
return true;
}
}
return false;
}
private readonly struct EditScope : IDisposable
{
private static readonly StringBuilder s_File = new StringBuilder();
private readonly string _path;
private readonly string _savePath;
public IEnumerable<string> lines => File.ReadLines(_path);
public StringBuilder sb => s_File;
public EditScope(string path, string savePath)
{
s_File.Length = 0;
_path = path;
_savePath = savePath;
}
public void Dispose()
{
}
public void Save()
{
File.WriteAllText(_savePath, sb.ToString());
}
}
}
}

View File

@ -0,0 +1,11 @@
fileFormatVersion: 2
guid: 431e5b95f2f5c4f19926a7fc5342e118
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:

View File

@ -0,0 +1,25 @@
using System.Text;
using System.Text.RegularExpressions;
namespace Coffee.UIParticleInternal.AssetModification
{
internal class TextReplaceModifier : ITextModifier
{
private readonly Regex _pattern;
private readonly string _replace;
public TextReplaceModifier(string pattern, string replace)
{
_pattern = new Regex(pattern);
_replace = replace;
}
public bool ModifyText(StringBuilder sb, string text)
{
if (!_pattern.IsMatch(text)) return false;
sb.AppendLine(_pattern.Replace(text, _replace));
return true;
}
}
}

View File

@ -0,0 +1,11 @@
fileFormatVersion: 2
guid: afc6ba9550e5e4c25951b03db43a4fd0
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant: