Skip to content
Merged

V2.0 #10

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
53 changes: 31 additions & 22 deletions .github/workflows/docfx-build-publish.yml
Original file line number Diff line number Diff line change
Expand Up @@ -23,22 +23,15 @@ jobs:
steps:
# Checks-out your repository under $GITHUB_WORKSPACE, so your job can access it
- name: Checkout
uses: actions/checkout@v2
uses: actions/checkout@v6.0.1

- name: Setup .NET Core
uses: actions/setup-dotnet@v1
- name: Setup .NET
uses: actions/setup-dotnet@v5.0.1
with:
dotnet-version: 3.1.101
dotnet-version: '8.0.x'

- name: Setup DocFX
uses: crazy-max/ghaction-chocolatey@v1
with:
args: install docfx --ignore-checksums

- name: Setup wkhtmltopdf
uses: crazy-max/ghaction-chocolatey@v1
with:
args: install wkhtmltopdf --ignore-checksums
run: dotnet tool install -g docfx --version 2.61.0

- name: Copy readme
run: cp .\README.md .\Tools~\DocFx\index.md
Expand All @@ -50,32 +43,48 @@ jobs:
continue-on-error: false

- name: Upload site artifact
uses: actions/upload-artifact@v2.2.4
uses: actions/upload-artifact@v6.0.0
with:
name: _site
path: Tools~/DocFx/_site

- name: Upload PDF
uses: actions/upload-artifact@v2.2.4
with:
name: _site_pdf
path: Tools~/DocFx/_site_pdf/DocFx.pdf

deploy_test:
needs: build
runs-on: ubuntu-latest
steps:
- name: Checkout
uses: actions/checkout@v6.0.1

- name: Download site artifact
uses: actions/download-artifact@v7.0.0
with:
name: _site
path: _site

- name: Publish
uses: peaceiris/actions-gh-pages@v4.0.0
with:
github_token: ${{ secrets.GITHUB_TOKEN }}
publish_branch: gh-pages-test
publish_dir: _site
force_orphan: true

deploy:
if: github.event_name == 'push'
needs: build
runs-on: ubuntu-latest
steps:
- name: Checkout
uses: actions/checkout@v2
uses: actions/checkout@v6.0.1

- name: Download site artifact
uses: actions/download-artifact@v1
uses: actions/download-artifact@v7.0.0
with:
name: _site
path: _site

- name: Publish
uses: peaceiris/actions-gh-pages@v3
uses: peaceiris/actions-gh-pages@v4.0.0
with:
github_token: ${{ secrets.GITHUB_TOKEN }}
publish_branch: gh-pages
Expand Down
7 changes: 7 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,12 @@
# Changelog

## 2.0.0
- Add asset release API
- Add async loading cancellation support
- Support UniTask for async loading
- Add editor tool for setting references through editor scripts
- Update documentation

## 1.0.0
- Improve scene reference, add auto fix button
- Use version defines
Expand Down
12 changes: 8 additions & 4 deletions Editor/SceneReferenceEditor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,17 @@
using UnityEditor;
using UnityEngine;

namespace SmartReference.Editor {
namespace SmartReference.Editor
{
[CustomPropertyDrawer(typeof(Runtime.SceneReference), true)]
internal class SceneReferenceEditor: PropertyDrawer {
internal class SceneReferenceEditor: PropertyDrawer
{
private SerializedProperty cacheProperty;
private SceneAsset currentScene;
private SerializedProperty scenePathProp;

public override void OnGUI(Rect position, SerializedProperty property, GUIContent label) {
public override void OnGUI(Rect position, SerializedProperty property, GUIContent label)
{
if (!SerializedProperty.EqualContents(property, cacheProperty)) {
cacheProperty = property;
scenePathProp = property.FindPropertyRelative("scenePath");
Expand Down Expand Up @@ -64,7 +67,8 @@ public override void OnGUI(Rect position, SerializedProperty property, GUIConten
}
}

public override float GetPropertyHeight(SerializedProperty property, GUIContent label) {
public override float GetPropertyHeight(SerializedProperty property, GUIContent label)
{
var height = EditorGUIUtility.singleLineHeight;
var scenePath = property.FindPropertyRelative("scenePath").stringValue;
var buildSettingsScenes = EditorBuildSettings.scenes;
Expand Down
2 changes: 1 addition & 1 deletion Editor/SmartReference.Editor.asmdef
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "SmartReference.Editor",
"rootNamespace": "SmartReference",
"rootNamespace": "SmartReference.Editor",
"includePlatforms": [
"Editor"
],
Expand Down
35 changes: 14 additions & 21 deletions Editor/SmartReferenceEditor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,17 +3,20 @@
using UnityEngine;
using Object = UnityEngine.Object;

namespace SmartReference.Editor {
namespace SmartReference.Editor
{
[CustomPropertyDrawer(typeof(Runtime.SmartReference), true)]
internal class SmartReferenceEditor: PropertyDrawer {
internal class SmartReferenceEditor: PropertyDrawer
{
private SerializedProperty cacheProperty;
private Object referencedObject;
private SerializedProperty guidProp;
private SerializedProperty fileIDProp;
private SerializedProperty pathProp;
private SerializedProperty typeProp;

public override void OnGUI(Rect position, SerializedProperty property, GUIContent label) {
public override void OnGUI(Rect position, SerializedProperty property, GUIContent label)
{
if (!SerializedProperty.EqualContents(property, cacheProperty)) {
cacheProperty = property;
referencedObject = null;
Expand All @@ -38,26 +41,16 @@ public override void OnGUI(Rect position, SerializedProperty property, GUIConten
}

var type = Type.GetType(typeProp.stringValue);
var referenced = EditorGUI.ObjectField(position, label, referencedObject, type, false);
if (referencedObject != referenced) {
if (referenced == null) {
guidProp.stringValue = string.Empty;
fileIDProp.longValue = 0;
pathProp.stringValue = string.Empty;
return;
var newReferenced = EditorGUI.ObjectField(position, label, referencedObject, type, false);
if (referencedObject != newReferenced) {
referencedObject = newReferenced;
if (newReferenced == null) {
SmartReferenceTool.ClearReference(property);
}

if (!AssetDatabase.TryGetGUIDAndLocalFileIdentifier(referenced, out var guid, out long fileID)) {
Debug.LogError(
$"[SmartReferenceEditor] Failed to get guid and fileID, path: {AssetDatabase.GetAssetPath(referenced)}");
return;
else
{
SmartReferenceTool.SetReference(property, newReferenced);
}

guidProp.stringValue = guid;
fileIDProp.longValue = fileID;
pathProp.stringValue = AssetDatabase.GetAssetPath(referenced);

referencedObject = referenced;
}
}
}
Expand Down
6 changes: 4 additions & 2 deletions Editor/SmartReferencePreBuildProcessor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,10 @@
using UnityEditor.Build.Reporting;
using UnityEngine;

namespace SmartReference.Editor {
public class SmartReferencePreBuildProcessor: IPreprocessBuildWithReport {
namespace SmartReference.Editor
{
public class SmartReferencePreBuildProcessor: IPreprocessBuildWithReport
{
public int callbackOrder => 0;

public void OnPreprocessBuild(BuildReport report) {
Expand Down
77 changes: 77 additions & 0 deletions Editor/SmartReferenceTool.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
using System;
using UnityEditor;
using Object = UnityEngine.Object;

namespace SmartReference.Editor
{
public static class SmartReferenceTool
{
public static void SetReference(SerializedProperty smartReferenceProperty, Object obj)
{
if (smartReferenceProperty == null)
{
throw new ArgumentNullException(nameof(smartReferenceProperty));
}

if (obj == null)
{
ClearReference(smartReferenceProperty);
return;
}

EnsureIsSmartReferenceProperty(smartReferenceProperty);

var typeProp = smartReferenceProperty.FindPropertyRelative("type");
var type = Type.GetType(typeProp.stringValue);
if (obj.GetType() != type)
{
throw new ArgumentException($"Object type does not match SmartReference type. Expected: {type}, Actual: {obj.GetType()}");
}

if (!AssetDatabase.TryGetGUIDAndLocalFileIdentifier(obj, out var guid, out long fileID))
{
throw new ArgumentException($"Object is not a valid asset with GUID/fileID: {obj}");
}

var path = AssetDatabase.GetAssetPath(obj);
smartReferenceProperty.FindPropertyRelative("guid").stringValue = guid;
smartReferenceProperty.FindPropertyRelative("fileID").longValue = fileID;
smartReferenceProperty.FindPropertyRelative("path").stringValue = path;

smartReferenceProperty.serializedObject.ApplyModifiedProperties();
}

/// <summary>
/// Clears SmartReference fields (guid/fileID/path/type).
/// </summary>
public static void ClearReference(SerializedProperty smartReferenceProperty)
{
if (smartReferenceProperty == null)
throw new ArgumentNullException(nameof(smartReferenceProperty));

EnsureIsSmartReferenceProperty(smartReferenceProperty);

smartReferenceProperty.FindPropertyRelative("guid").stringValue = string.Empty;
smartReferenceProperty.FindPropertyRelative("fileID").longValue = 0;
smartReferenceProperty.FindPropertyRelative("path").stringValue = string.Empty;

smartReferenceProperty.serializedObject.ApplyModifiedProperties();
}

private static void EnsureIsSmartReferenceProperty(SerializedProperty p)
{
// We check presence of the expected fields rather than type name, so it works with SmartReference<T>
// stored in any container.
if (p.propertyType != SerializedPropertyType.Generic)
throw new ArgumentException($"Property is not a serialized object/struct: {p.propertyPath}, type: {p.propertyType}");

if (p.FindPropertyRelative("guid") == null ||
p.FindPropertyRelative("fileID") == null ||
p.FindPropertyRelative("path") == null ||
p.FindPropertyRelative("type") == null)
{
throw new ArgumentException($"Property does not look like a SmartReference (missing guid/fileID/path/type): {p.propertyPath}");
}
}
}
}
3 changes: 3 additions & 0 deletions Editor/SmartReferenceTool.cs.meta

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

22 changes: 14 additions & 8 deletions Editor/SmartReferenceUtils.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,17 @@
using UnityEditor;
using UnityEngine;

namespace SmartReference.Editor {
public static class SmartReferenceUtils {
namespace SmartReference.Editor
{
public static class SmartReferenceUtils
{
[MenuItem("Tools/SmartReference/Update All References", priority = 100)]
public static void UpdateAllReferences() {
public static void UpdateAllReferences()
{
EditorUtility.DisplayProgressBar("SmartReference", "Updating all references...", 0);

try {
try
{
var typeList = GetTypesWithSpecificField(typeof(Runtime.SmartReference));
foreach (var type in typeList) {
var guids = AssetDatabase.FindAssets($"t:{type.Name}");
Expand All @@ -37,7 +41,8 @@ public static void UpdateAllReferences() {
}
}

public static void UpdateReference(this Runtime.SmartReference smartReference) {
public static void UpdateReference(this Runtime.SmartReference smartReference)
{
if (smartReference == null || string.IsNullOrEmpty(smartReference.guid)) return;

var succeed = false;
Expand Down Expand Up @@ -68,7 +73,8 @@ public static void UpdateReference(this Runtime.SmartReference smartReference) {
}
}

internal static void UpdateReferenceWithProperty(SerializedProperty property) {
internal static void UpdateReferenceWithProperty(SerializedProperty property)
{
if (property == null) return;

var guidProp = property.FindPropertyRelative("guid");
Expand Down Expand Up @@ -110,15 +116,15 @@ private static List<Type> GetTypesWithSpecificField(Type fieldType)
List<Type> result = new List<Type>();

// Iterating over all assemblies
foreach (Assembly assembly in AppDomain.CurrentDomain.GetAssemblies())
foreach (var assembly in AppDomain.CurrentDomain.GetAssemblies())
{
// Optional: Filter the assemblies by name if necessary
var fullName = assembly.FullName;
if (fullName.StartsWith("UnityEngine.") || fullName.StartsWith("UnityEditor.") ||
fullName.StartsWith("Unity.") || fullName.StartsWith("Bee.") ||
fullName.StartsWith("System.") || fullName.StartsWith("Mono.")) continue;

foreach (Type type in assembly.GetTypes())
foreach (var type in assembly.GetTypes())
{
// Safeguard against types that might throw exceptions
try
Expand Down
Loading