Skip to content

Commit

Permalink
Rework reward definition
Browse files Browse the repository at this point in the history
  • Loading branch information
Guerra24 committed May 28, 2024
1 parent c5d96c6 commit 8db2a6d
Show file tree
Hide file tree
Showing 16 changed files with 112 additions and 66 deletions.
8 changes: 8 additions & 0 deletions Assets/Editor/Actions.cs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
using AvatarViewer.SDK;
using UnityEditor;

namespace AvatarViewer.Editor
Expand All @@ -7,6 +8,13 @@ public static class Actions
[MenuItem("Avatar Viewer/Generate built-in rewards")]
public static void BuildRewards()
{
foreach (var asset in AssetDatabase.GetAssetPathsFromAssetBundle("builtin-rewards"))
{
var rewardAssetInfo = AssetDatabase.LoadAssetAtPath<RewardAssetInfo>(asset);
rewardAssetInfo.Prefab.GetComponent<RewardAsset>().Info = rewardAssetInfo;
}
AssetDatabase.SaveAssets();

BuildPipeline.BuildAssetBundles("Assets/StreamingAssets", BuildAssetBundleOptions.StrictMode | BuildAssetBundleOptions.ChunkBasedCompression, EditorUserBuildSettings.activeBuildTarget);
}
}
Expand Down
19 changes: 19 additions & 0 deletions Assets/Rewards/Objects/Box/Box.asset
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
%YAML 1.1
%TAG !u! tag:unity3d.com,2011:
--- !u!114 &11400000
MonoBehaviour:
m_ObjectHideFlags: 0
m_CorrespondingSourceObject: {fileID: 0}
m_PrefabInstance: {fileID: 0}
m_PrefabAsset: {fileID: 0}
m_GameObject: {fileID: 0}
m_Enabled: 1
m_EditorHideFlags: 0
m_Script: {fileID: 11500000, guid: 8593682cb7a6dfd4b83ac783925009cb, type: 3}
m_Name: Box
m_EditorClassIdentifier:
Guid:
serializedGuid: 158dcd30-65e2-4947-84b1-6776a712a052
AssetName: Box
DisablePhysicsOnCollision: 0
Prefab: {fileID: 4635790230465575798, guid: 40e23b62cc9ebfe4a986b9d73665a741, type: 3}
8 changes: 8 additions & 0 deletions Assets/Rewards/Objects/Box/Box.asset.meta

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

5 changes: 1 addition & 4 deletions Assets/Rewards/Objects/Box/Box.prefab
Original file line number Diff line number Diff line change
Expand Up @@ -367,7 +367,4 @@ MonoBehaviour:
m_Script: {fileID: 11500000, guid: 19d64d4e41648a147ad98e67f6d7d0c4, type: 3}
m_Name:
m_EditorClassIdentifier:
Guid:
serializedGuid: 158dcd30-65e2-4947-84b1-6776a712a052
Name: Box
DisablePhysicsOnCollision: 0
Info: {fileID: 0}
2 changes: 1 addition & 1 deletion Assets/Rewards/Objects/Box/Box.prefab.meta

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

18 changes: 9 additions & 9 deletions Assets/Scenes/Main.unity
Original file line number Diff line number Diff line change
Expand Up @@ -1407,12 +1407,12 @@ PrefabInstance:
- target: {fileID: 70972866194739485, guid: d2bf08666f6585c4fb2de39208019ae2,
type: 3}
propertyPath: m_LocalPosition.x
value: 0
value: -250
objectReference: {fileID: 0}
- target: {fileID: 70972866194739485, guid: d2bf08666f6585c4fb2de39208019ae2,
type: 3}
propertyPath: m_LocalPosition.y
value: 0
value: -304
objectReference: {fileID: 0}
- target: {fileID: 3183264326573952452, guid: d2bf08666f6585c4fb2de39208019ae2,
type: 3}
Expand Down Expand Up @@ -1602,12 +1602,12 @@ PrefabInstance:
- target: {fileID: 4914328431171304639, guid: d2bf08666f6585c4fb2de39208019ae2,
type: 3}
propertyPath: m_LocalPosition.x
value: 0
value: -250
objectReference: {fileID: 0}
- target: {fileID: 4914328431171304639, guid: d2bf08666f6585c4fb2de39208019ae2,
type: 3}
propertyPath: m_LocalPosition.y
value: 0
value: 304
objectReference: {fileID: 0}
- target: {fileID: 6974992526447935581, guid: d2bf08666f6585c4fb2de39208019ae2,
type: 3}
Expand All @@ -1632,7 +1632,7 @@ PrefabInstance:
- target: {fileID: 7071451658218752593, guid: d2bf08666f6585c4fb2de39208019ae2,
type: 3}
propertyPath: m_LocalPosition.y
value: 0
value: 304
objectReference: {fileID: 0}
- target: {fileID: 8211928304835922972, guid: d2bf08666f6585c4fb2de39208019ae2,
type: 3}
Expand Down Expand Up @@ -5886,7 +5886,7 @@ PrefabInstance:
- target: {fileID: 70972866194739485, guid: d2bf08666f6585c4fb2de39208019ae2,
type: 3}
propertyPath: m_LocalPosition.y
value: 0
value: -304
objectReference: {fileID: 0}
- target: {fileID: 3183264326573952452, guid: d2bf08666f6585c4fb2de39208019ae2,
type: 3}
Expand Down Expand Up @@ -6076,7 +6076,7 @@ PrefabInstance:
- target: {fileID: 4914328431171304639, guid: d2bf08666f6585c4fb2de39208019ae2,
type: 3}
propertyPath: m_LocalPosition.y
value: 0
value: 304
objectReference: {fileID: 0}
- target: {fileID: 6974992526447935581, guid: d2bf08666f6585c4fb2de39208019ae2,
type: 3}
Expand All @@ -6101,12 +6101,12 @@ PrefabInstance:
- target: {fileID: 7071451658218752593, guid: d2bf08666f6585c4fb2de39208019ae2,
type: 3}
propertyPath: m_LocalPosition.x
value: 0
value: 250
objectReference: {fileID: 0}
- target: {fileID: 7071451658218752593, guid: d2bf08666f6585c4fb2de39208019ae2,
type: 3}
propertyPath: m_LocalPosition.y
value: 0
value: 304
objectReference: {fileID: 0}
- target: {fileID: 8211928304835922972, guid: d2bf08666f6585c4fb2de39208019ae2,
type: 3}
Expand Down
18 changes: 3 additions & 15 deletions Assets/Scripts/ApplicationState.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ public static class ApplicationState

public static Dictionary<string, AudioClip> ExternalAudios { get; } = new();

public static Dictionary<Guid, LoadedRewardAsset> RewardAssets { get; } = new();
public static Dictionary<Guid, RewardAssetInfo> RewardAssets { get; } = new();

public static Dictionary<Guid, LoadedRewardAssetsBundle> RewardBundles { get; } = new();

Expand All @@ -38,25 +38,13 @@ public LoadedAvatar(AssetBundle bundle, GameObject @object)
}
}

public class LoadedRewardAsset
{
public GameObject Object { get; }
public RewardAsset RewardAsset { get; }

public LoadedRewardAsset(GameObject @object, RewardAsset rewardAsset)
{
Object = @object;
RewardAsset = rewardAsset;
}
}

public class LoadedRewardAssetsBundle
{
public AssetBundle Bundle { get; }

public Dictionary<Guid, LoadedRewardAsset> RewardAssets { get; }
public Dictionary<Guid, RewardAssetInfo> RewardAssets { get; }

public LoadedRewardAssetsBundle(AssetBundle bundle, Dictionary<Guid, LoadedRewardAsset> rewardAssets)
public LoadedRewardAssetsBundle(AssetBundle bundle, Dictionary<Guid, RewardAssetInfo> rewardAssets)
{
Bundle = bundle;
RewardAssets = rewardAssets;
Expand Down
2 changes: 1 addition & 1 deletion Assets/Scripts/RewardSpawner.cs
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ private async UniTaskVoid UpdateAsync()
{
var spawnPoint = GetSpawn(reward.SpawnPoint);
if (ApplicationState.RewardAssets.TryGetValue(reward.RewardAsset, out var asset))
SetupReward(Instantiate(asset.Object, spawnPoint.position, spawnPoint.rotation * asset.Object.transform.rotation), spawnPoint, reward);
SetupReward(Instantiate(asset.Prefab, spawnPoint.position, spawnPoint.rotation * asset.Prefab.transform.rotation), spawnPoint, reward);
await UniTask.Delay(200);
}
_processingRewards = false;
Expand Down
12 changes: 5 additions & 7 deletions Assets/Scripts/Startup.cs
Original file line number Diff line number Diff line change
Expand Up @@ -144,16 +144,14 @@ await assetBundle.LoadAssetAsync<GameObject>("VSFAvatar").ToUniTask().ContinueWi
foreach (var kvp in bundles.Union(new Dictionary<Guid, RewardAssetsBundle>() { { Guid.Empty, new RewardAssetsBundle(Path.Combine(Application.streamingAssetsPath, "builtin-rewards")) } }).ToDictionary(k => k.Key, v => v.Value))
{
var bundle = await AssetBundle.LoadFromFileAsync(kvp.Value.Path);
var request = bundle.LoadAllAssetsAsync<GameObject>();
var request = bundle.LoadAllAssetsAsync<RewardAssetInfo>();
await request;
var rewardAssets = new Dictionary<Guid, LoadedRewardAsset>();
var rewardAssets = new Dictionary<Guid, RewardAssetInfo>();
foreach (var @object in request.allAssets)
{
var reward = @object as GameObject;
var rewardAsset = reward.GetComponent<RewardAsset>();
var lra = new LoadedRewardAsset(reward, rewardAsset);
ApplicationState.RewardAssets.Add(rewardAsset.Guid, lra);
rewardAssets.Add(rewardAsset.Guid, lra);
var reward = @object as RewardAssetInfo;
ApplicationState.RewardAssets.Add(reward.Guid, reward);
rewardAssets.Add(reward.Guid, reward);
}
ApplicationState.RewardBundles.Add(kvp.Key, new LoadedRewardAssetsBundle(bundle, rewardAssets));
}
Expand Down
2 changes: 1 addition & 1 deletion Assets/Scripts/UI/Items/ItemRewardListItem.cs
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ protected override void Awake()
base.Awake();
SpawnPoint.AddOptions(Enum.GetNames(typeof(ItemRewardSpawnPoint)).ToList());
foreach (var rewardAsset in ApplicationState.RewardAssets)
RewardAsset.options.Add(new GuidDropdownData(rewardAsset.Value.RewardAsset.Name, rewardAsset.Key));
RewardAsset.options.Add(new GuidDropdownData(rewardAsset.Value.AssetName, rewardAsset.Key));
RewardAsset.RefreshShownValue();

Volume.SetupSlider((value) => Reward.Volume = value);
Expand Down
2 changes: 1 addition & 1 deletion Assets/Scripts/UI/Items/RewardAssetBundleListItem.cs
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ private async UniTaskVoid RemoveBundle()
var tcs = new UniTaskCompletionSource<bool>();
_dialog.CreateDialog(
title,
content.AsFormat(rewardAsset.RewardAsset.Name, itemReward.Title),
content.AsFormat(rewardAsset.AssetName, itemReward.Title),
() => { itemReward.RewardAsset = Guid.Parse("158dcd30-65e2-4947-84b1-6776a712a052"); tcs.TrySetResult(false); },
() => tcs.TrySetResult(true));
if (await tcs.Task)
Expand Down
10 changes: 4 additions & 6 deletions Assets/Scripts/UI/Settings/RewardAssetBundlesPage.cs
Original file line number Diff line number Diff line change
Expand Up @@ -40,14 +40,12 @@ private async UniTaskVoid AddBundle(string path)
var bundle = await AssetBundle.LoadFromFileAsync(path);
var request = bundle.LoadAllAssetsAsync<GameObject>();
await request;
var rewardAssets = new Dictionary<Guid, LoadedRewardAsset>();
var rewardAssets = new Dictionary<Guid, RewardAssetInfo>();
foreach (var @object in request.allAssets)
{
var reward = @object as GameObject;
var rewardAsset = reward.GetComponent<RewardAsset>();
var lra = new LoadedRewardAsset(reward, rewardAsset);
ApplicationState.RewardAssets.Add(rewardAsset.Guid, lra);
rewardAssets.Add(rewardAsset.Guid, lra);
var reward = @object as RewardAssetInfo;
ApplicationState.RewardAssets.Add(reward.Guid, reward);
rewardAssets.Add(reward.Guid, reward);
}
var rab = new RewardAssetsBundle(path);
var guid = Guid.NewGuid();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,25 +41,39 @@ private void OnGUI()
bool valid = true;
foreach (var asset in assets)
{
var gameObject = AssetDatabase.LoadAssetAtPath<GameObject>(asset);
var hasRewardAsset = gameObject.TryGetComponent<RewardAsset>(out var rewardAsset);
var hasRigidBody = gameObject.TryGetComponent<Rigidbody>(out var rigidbody);
var hasComponents = hasRewardAsset && hasRigidBody;
if (!hasComponents)
valid = false;

if (EditorGUILayout.Foldout(true, hasRewardAsset ? rewardAsset.Name : gameObject.name))
var rewardAssetInfo = AssetDatabase.LoadAssetAtPath<RewardAssetInfo>(asset);
if (EditorGUILayout.Foldout(true, rewardAssetInfo.AssetName))
{
EditorGUILayout.BeginHorizontal();
GUILayout.Space(16f);
EditorGUILayout.BeginVertical();
EditorGUILayout.ObjectField(gameObject, typeof(GameObject), false);
if (!hasRewardAsset)
GUILayout.Label("Missing RewardAsset component");
if (!hasRigidBody)
GUILayout.Label("Missing RigidBody component");
if (hasComponents)
GUILayout.Label("Reward valid");
if (rewardAssetInfo.Prefab != null)
{
var hasRewardAsset = rewardAssetInfo.Prefab.TryGetComponent<RewardAsset>(out var rewardAsset);
var hasRigidBody = rewardAssetInfo.Prefab.TryGetComponent<Rigidbody>(out var rigidbody);
var hasComponents = hasRewardAsset && hasRigidBody;
if (!hasComponents)
valid = false;

if (rewardAsset.Info != rewardAssetInfo)
{
rewardAsset.Info = rewardAssetInfo;
AssetDatabase.SaveAssets();
}

UnityEditor.Editor.CreateEditor(rewardAssetInfo).OnInspectorGUI();
if (!hasRewardAsset)
GUILayout.Label("Missing RewardAsset component");
if (!hasRigidBody)
GUILayout.Label("Missing RigidBody component");
if (hasComponents)
GUILayout.Label("Reward valid");
}
else
{
valid = false;
GUILayout.Label("Missing Prefab");
}
EditorGUILayout.EndVertical();
EditorGUILayout.EndHorizontal();
}
Expand Down
9 changes: 3 additions & 6 deletions Packages/net.guerra24.avatarviewer.sdk/Runtime/RewardAsset.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,8 @@ namespace AvatarViewer.SDK
{
public class RewardAsset : MonoBehaviour
{
public SerializableGuid Guid;

public string Name;

public bool DisablePhysicsOnCollision;
[HideInInspector]
public RewardAssetInfo Info;

private Rigidbody Rigidbody;
private AudioSource AudioSource;
Expand All @@ -21,7 +18,7 @@ private void Start()

private void OnCollisionEnter(Collision collision)
{
if (DisablePhysicsOnCollision)
if (Info.DisablePhysicsOnCollision)
{
Rigidbody.isKinematic = true;
Rigidbody.detectCollisions = false;
Expand Down
17 changes: 17 additions & 0 deletions Packages/net.guerra24.avatarviewer.sdk/Runtime/RewardAssetInfo.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
using UnityEngine;

namespace AvatarViewer.SDK
{
[CreateAssetMenu(fileName = "RewardAssetInfo", menuName = "Avatar Viewer/RewardAssetInfo")]
public class RewardAssetInfo : ScriptableObject
{
public SerializableGuid Guid;

public string AssetName;

public bool DisablePhysicsOnCollision;

public GameObject Prefab;

}
}

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

0 comments on commit 8db2a6d

Please sign in to comment.