Astra Unity 2.6.6
Astra Unity Plugin
Examples/GettingStarted/GettingStarted.cs
using Astra.Asset;
using Astra.Core;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
namespace Astra.Examples
{
public class GettingStarted : MonoBehaviour
{
public Button ButtonInitialize;
public Text InitializeText;
public Text AvatarText;
public Button ButtonSearchAvatars;
public Button ButtonSearchFigures;
public Button ButtonSearchCloth;
public Button ButtonSearchDecals;
public Button ButtonSearchMorphable;
public Text LastSearchAssetsText;
public Button ButtonLoadNextAsset;
public Text LoadNextAssetText;
public Button ButtonLoadNextAvatar;
public Text AvatarLoadText;
public Button ButtonClearItems;
public TextAsset AppConfig;
public Helpers.ProgressBar ProgressBar;
private GameObject _rootGO;
private AvatarItem _avatarItem;
private List<CatalogEntry> _avatarList = null;
private List<CatalogEntry> _lastAssetSearch = null;
private int _assetIndex = 0;
private int _avatarIndex = 0;
private string _assetSearchType;
// Use this for initialization
void Start()
{
//Link up our buttons with their triggers
}
#region event management
protected void BindEvents()
{
ButtonInitialize.onClick.AddListener(HandleButtonInitialize);
ButtonSearchAvatars.onClick.AddListener(HandleButtonSearchAvatars);
ButtonSearchFigures.onClick.AddListener(HandleButtonSearchFigures);
ButtonLoadNextAsset.onClick.AddListener(HandleButtonLoadNextAsset);
ButtonSearchCloth.onClick.AddListener(HandleButtonSearchCloth);
ButtonSearchDecals.onClick.AddListener(HandleButtonSearchDecals);
ButtonSearchMorphable.onClick.AddListener(HandleButtonSearchMorphables);
ButtonLoadNextAvatar.onClick.AddListener(HandleButtonLoadNextAvatar);
ButtonClearItems.onClick.AddListener(HandleButtonClearItems);
}
private void HandleButtonInitialize()
{
InitializeText.text = "Initializing...";
StartCoroutine(InitializeCoroutine());
}
private IEnumerator InitializeCoroutine()
{
AstraManager.StartupAstra();
yield return AstraManager.WaitForInit();
Configuration.ProjectConfigError error = Configuration.ProjectConfigError.Success;
yield return Configuration.Initialize(AppConfig).AsCoroutine(null, null, e => error = e);
if (error == Configuration.ProjectConfigError.Success)
{
InitializeText.text = "Initialized";
}
else
{
InitializeText.text = "Init Failed: " + error.ToString();
}
}
private void HandleButtonSearchAvatars()
{
AvatarText.text = "Searching...";
StartCoroutine(Catalog.GetAvatars().AsCoroutine(CallbackProgress,
// Callback when the avatar list is loaded
(avatarList) =>
{
_avatarList = avatarList;
Astra.Logger.Log("Found: " + avatarList.Count);
AvatarText.text = avatarList.Count + " Avatars";
},
CallbackCatalogError));
}
private void HandleButtonSearchFigures()
{
LastSearchAssetsText.text = "Searching for figures...";
_assetSearchType = "Figure";
StartCoroutine(Catalog.SearchForFigures(_avatarItem).AsCoroutine(CallbackProgress, CallbackAssetListLoaded, CallbackCatalogError));
}
private void HandleButtonSearchCloth()
{
LastSearchAssetsText.text = "Searching for clothing...";
_assetSearchType = "Clothing";
StartCoroutine(Catalog.SearchForClothing(_avatarItem, null).AsCoroutine(CallbackProgress, CallbackAssetListLoaded, CallbackCatalogError));
}
private void HandleButtonSearchDecals()
{
LastSearchAssetsText.text = "Searching for decals...";
_assetSearchType = "Decal";
StartCoroutine(Catalog.SearchForDecals(_avatarItem, null).AsCoroutine(CallbackProgress, CallbackAssetListLoaded, CallbackCatalogError));
}
private void HandleButtonSearchMorphables()
{
LastSearchAssetsText.text = "Searching for morphs...";
_assetSearchType = "Morph";
StartCoroutine(Catalog.SearchForMorphables(_avatarItem).AsCoroutine(CallbackProgress, CallbackAssetListLoaded, CallbackCatalogError));
}
private void HandleButtonLoadNextAsset()
{
LoadNextAssetText.text = "Loading...";
if (_lastAssetSearch == null || _lastAssetSearch.Count <= 0)
{
LoadNextAssetText.text = "No assets to load";
return;
}
if (_assetIndex >= _lastAssetSearch.Count)
{
_assetIndex = 0;
}
ButtonLoadNextAsset.enabled = false;
var catalogEntry = _lastAssetSearch[_assetIndex];
LoadNextAssetText.text = "Loading: " + catalogEntry.Name;
StartCoroutine(_avatarItem.ApplyAsset(catalogEntry).AsCoroutine(CallbackProgress,
() =>
{
LoadNextAssetText.text = "Loaded: " + catalogEntry.Name;
ButtonLoadNextAsset.enabled = true;
},
() => { Astra.Logger.LogError("Failed to load asset: " + catalogEntry.Name); }));
_assetIndex++;
}
private void HandleButtonLoadNextAvatar()
{
if (_avatarList == null || _avatarList.Count <= 0)
{
Astra.Logger.LogWarning("No app avatars found, make sure you search them first");
}
if (_avatarIndex >= _avatarList.Count)
{
_avatarIndex = 0;
}
var guid = _avatarList[_avatarIndex].Guid;
Astra.Logger.Log("Loading avatar: " + guid);
AvatarLoadText.text = "Loading: " + guid;
StartCoroutine(Loader.LoadAvatar(guid).AsCoroutine(CallbackProgress,
(avatarItem) => {
Astra.Logger.Log("Avatar loaded");
AvatarLoadText.text = "Loaded: " + guid;
_avatarItem = avatarItem;
if (_rootGO != null)
{
GameObject.Destroy(_rootGO);
}
_rootGO = _avatarItem.gameObject;
},
(error) => { Astra.Logger.LogError("Failed loading avatar: " + error); }));
_avatarIndex++;
}
private void HandleButtonClearItems()
{
if (_avatarItem == null)
{
Astra.Logger.LogError("You need an avatar to use this");
return;
}
_avatarItem.RemoveAllItems();
}
#endregion
#region callbacks
private void CallbackProgress(float progress)
{
ProgressBar.UpdatePercent(progress);
}
private void CallbackCatalogError(Catalog.CatalogError catalogError)
{
Astra.Logger.LogError("Error when fetching assets: " + catalogError.ToString());
}
private void CallbackAssetListLoaded(List<CatalogEntry> assetList)
{
_lastAssetSearch = assetList;
var logMessage = "Found: " + assetList.Count + " " + _assetSearchType + " assets";
Astra.Logger.Log(logMessage);
LastSearchAssetsText.text = logMessage;
}
#endregion
}
}
Text LastSearchAssetsText
Used when displaying info about searching figures
Definition: GettingStarted.cs:64
Button ButtonLoadNextAsset
Loads the next asset from the search results
Definition: GettingStarted.cs:69
void BindEvents()
Helper to link various ui elements to their actions
Definition: GettingStarted.cs:128
Text AvatarText
Used to display the avatar info
Definition: GettingStarted.cs:34
Button ButtonSearchCloth
Finds cloth items compatible with the current figure
Definition: GettingStarted.cs:49
Text InitializeText
Used to show when the Astra system has been initialized
Definition: GettingStarted.cs:29
Button ButtonInitialize
Clicking this button will create our base astra gameobject, we need this in all scenes You can skip t...
Definition: GettingStarted.cs:24
TextAsset AppConfig
Drag your config file into this slot (or load in code)
Definition: GettingStarted.cs:95
Button ButtonSearchMorphable
Finds compatible morphables
Definition: GettingStarted.cs:59
Text LoadNextAssetText
Displays info when loading an asset from a search
Definition: GettingStarted.cs:74
Button ButtonSearchAvatars
Finds avatars and prints list
Definition: GettingStarted.cs:39
Button ButtonLoadNextAvatar
Loads a complete avatar
Definition: GettingStarted.cs:79
Button ButtonSearchDecals
Finds decals compatible with the current figure
Definition: GettingStarted.cs:54
Button ButtonClearItems
Removes items on the avatar except the figure
Definition: GettingStarted.cs:90
Text AvatarLoadText
Info about the loading avatar
Definition: GettingStarted.cs:84
Button ButtonSearchFigures
Finds figures and prints list
Definition: GettingStarted.cs:44
Handles Astra logging messages in C# allowing filtering and redirection
Definition: Logger.cs:15
static void LogError(object message)
A one-to-one with UnityEngine.Debug.LogError, records as Info level
Definition: Logger.cs:115
static void LogWarning(object message)
A one-to-one with UnityEngine.Debug.LogWarning, records as Info level
Definition: Logger.cs:107
static void Log(object message)
A one-to-one with UnityEngine.Debug.Log, records as Info level
Definition: Logger.cs:99
Definition: AnimationManager.cs:10
Definition: DB.cs:5
Definition: AnimatingAnAvatar.cs:9
Definition: AnimationManager.cs:10