using UnityEngine;
using ORKFramework;
#if UNITY_EDITOR
using UnityEditor;
#endif
public class orkHandlerUpdater : MonoBehaviour
{
#if UNITY_EDITOR
private ORKHandler OrkHandler;
private bool refreshInspectorRequested = false;
private float timeSinceLastInspectorUpdate = 0f;
private float inspectorUpdateInterval = 0.1f; // Set the desired update interval in seconds.
private void Start()
{
GameObject Ork = GameObject.Find("_ORK");
if (Ork != null)
{
OrkHandler = Ork.GetComponent<ORKHandler>();
}
}
private void Update()
{
// Request an Inspector refresh
RequestInspectorRefresh();
}
private void RequestInspectorRefresh()
{
if (OrkHandler != null)
{
timeSinceLastInspectorUpdate += Time.deltaTime;
if (timeSinceLastInspectorUpdate >= inspectorUpdateInterval)
{
timeSinceLastInspectorUpdate = 0f;
// Request the Inspector to refresh.
// This will make the changes visible in the Inspector.
refreshInspectorRequested = true;
EditorApplication.update += RefreshInspector;
}
}
}
private void RefreshInspector()
{
if (refreshInspectorRequested && OrkHandler != null)
{
refreshInspectorRequested = false;
// Use the SetDirty method to refresh the Inspector.
UnityEditor.EditorUtility.SetDirty(OrkHandler.gameObject);
}
}
#endif
}
using System;
using System.Collections.Generic;
using System.Text.RegularExpressions;
using UnityEngine;
using System.IO;
using System.Linq;
using System.Text;
using GamingIsLove.Makinom;
using GamingIsLove.ORKFramework;
#if UNITY_EDITOR
using UnityEditor;
#endif
// Thanks to Keldryn
// http://forum.orkframework.com/discussion/3336/resource-generating-ork-constants-for-scripting#latest
namespace RustedGames
{
#if UNITY_EDITOR
public static class MakinORKConstantsGenerator
{
[MenuItem("Edit/Generate MakinORKConstants.cs")]
public static void Generate()
{
// Try to find an existing file in the project called "ORKConstants.cs"
string filePath = string.Empty;
foreach (var file in Directory.GetFiles(Application.dataPath, "*.cs", SearchOption.AllDirectories))
{
if (Path.GetFileNameWithoutExtension(file) == "MakinORKConstants")
{
filePath = file;
break;
}
}
// If no such file exists already, use the save panel to get a folder in which the file will be placed.
if (string.IsNullOrEmpty(filePath))
{
string directory = EditorUtility.OpenFolderPanel("Choose location for MakinORKConstants.cs", Application.dataPath, "");
// Canceled choose? Do nothing.
if (string.IsNullOrEmpty(directory))
{
return;
}
filePath = Path.Combine(directory, "MakinORKConstants.cs");
}
// Ensure that ORK is instantiated
if (!Maki.Initialized)
{
Maki.Initialize(GamingIsLove.Makinom.Editor.MakinomAssetHelper.LoadProjectAsset());
}
Debug.Log("Generating MakinORKConstants.cs...");
// Write out our file
using (var writer = new StreamWriter(filePath))
{
writer.WriteLine("// This file is auto-generated. Modifications are not saved.");
writer.WriteLine();
writer.WriteLine("namespace RustedGames");
writer.WriteLine("{");
// Write out the tags
writer.WriteLine(BuildClass("ORKStatusValue", ORK.StatusValues.GetNames().ToList()));
writer.WriteLine(BuildClass("ORKStatusEffect", ORK.StatusEffects.GetNames().ToList()));
writer.WriteLine(BuildClass("ORKAbility", ORK.Abilities.GetNames().ToList()));
writer.WriteLine(BuildClass("ORKEquipmentSlots", ORK.EquipmentSlots.GetNames().ToList()));
writer.WriteLine(BuildClass("ORKEquipment", ORK.Equipment.GetNames().ToList()));
// Custom
writer.WriteLine(BuildClass("ORKCombatants", ORK.Combatants.GetNames().ToList()));
writer.WriteLine(BuildClass("ORKItemTypes", ORK.ItemTypes.GetNames().ToList()));
writer.WriteLine(BuildClass("ORKItems", ORK.Items.GetNames().ToList()));
writer.WriteLine(BuildClass("ORKCurrencies", ORK.Currencies.GetNames().ToList()));
writer.WriteLine(BuildClass("ORKInputKeys", Maki.InputKeys.GetNames().ToList()));
writer.WriteLine(BuildClass("ORKControlMaps", ORK.ControlMaps.GetNames().ToList()));
writer.WriteLine(BuildClass("ORKAbilityTypes", ORK.AbilityTypes.GetNames().ToList()));
writer.WriteLine(BuildClass("ORKAreaTypes", ORK.AreaTypes.GetNames().ToList()));
writer.WriteLine(BuildClass("ORKAreas", ORK.Areas.GetNames().ToList()));
writer.WriteLine(BuildClass("ORKDifficulties", ORK.Difficulties.GetNames().ToList()));
// End of namespace RustedGames
writer.WriteLine("}");
writer.WriteLine();
}
Debug.Log("Completed");
// Refresh
AssetDatabase.Refresh();
}
private static string BuildClass(string className, List<string> constantNames)
{
StringBuilder sb = new StringBuilder(" public static class ");
sb.AppendLine(className);
sb.AppendLine(" {");
try
{
for (int i = 0; i < constantNames.Count; i++)
{
sb.AppendLine(string.Format(" public const int {0} = {1};", MakeSafeForCode(constantNames[i].ToUpper()),
i));
}
}
catch (Exception ex)
{
Debug.LogError(string.Format("Could not generate class {0}: {1}", className, ex.Message));
}
finally
{
sb.AppendLine(" }");
sb.AppendLine();
}
return sb.ToString();
}
private static string MakeSafeForCode(string str)
{
str = Regex.Replace(str, "[^a-zA-Z0-9_]", "_", RegexOptions.Compiled);
if (char.IsDigit(str[0]))
{
str = "_" + str;
}
return str;
}
}
#endif //UNITY_EDITOR
}
public static void Save(){
int index = Maki.SaveGame.LatestSaveIndex;
if(index < 0) index = 0;
Maki.SaveGame.Save(index);
}
#if UNITY_2018_1_OR_NEWER
private void OnEnable()
{
Application.wantsToQuit -= OnWantsToQuit;
Application.wantsToQuit += OnWantsToQuit;
}
private void OnDisable()
{
Application.wantsToQuit -= OnWantsToQuit;
}
private bool OnWantsToQuit()
{
Save();
return true;
}
#else
/// <summary>
/// When quitting, save the game.
/// </summary>
private void OnApplicationQuit()
{
Save();
}
#endif
gamingislove said: Isn't the generation code editor-only anyway?Yes, it is. Just fixing what is technically an error in the code. If the one Editor using statement is inside #if UNITY_EDITOR then the other one should be too. ;-)
Combatant.Status[1].GetValue()
Combatant.Status[ORKStatusValue.HP].GetValue()
#if UNITY_EDITOR
using System;
using System.Collections.Generic;
using System.Text.RegularExpressions;
using UnityEngine;
using System.IO;
using System.Linq;
using System.Text;
using ORKFramework;
using UnityEditor;
using ORKFramework.Editor;
namespace MyGame
{
public static class ORKConstantsGenerator
{
[MenuItem("Edit/Generate ORKConstants.cs")]
public static void Generate()
{
// Try to find an existing file in the project called "ORKConstants.cs"
string filePath = string.Empty;
foreach (var file in Directory.GetFiles(Application.dataPath, "*.cs", SearchOption.AllDirectories)) {
if (Path.GetFileNameWithoutExtension(file) == "ORKConstants") {
filePath = file;
break;
}
}
// If no such file exists already, use the save panel to get a folder in which the file will be placed.
if (string.IsNullOrEmpty(filePath)) {
string directory = EditorUtility.OpenFolderPanel("Choose location for ORKConstants.cs", Application.dataPath, "");
// Canceled choose? Do nothing.
if (string.IsNullOrEmpty(directory)) {
return;
}
filePath = Path.Combine(directory, "ORKConstants.cs");
}
// Ensure that ORK is instantiated
if (!ORK.Instantiated)
{
ORK.Initialize(ORKAssetHelper.LoadORKProject());
}
Debug.Log("Generating ORKConstants.cs...");
// Write out our file
using (var writer = new StreamWriter(filePath)) {
writer.WriteLine("// This file is auto-generated. Modifications are not saved.");
writer.WriteLine();
writer.WriteLine("namespace MyGame");
writer.WriteLine("{");
// Write out the tags
writer.WriteLine(BuildClass("CombatantStatusValue", ORK.StatusValues.GetNames(false).ToList()));
writer.WriteLine(BuildClass("CombatantStatusEffect", ORK.StatusEffects.GetNames(false).ToList()));
writer.WriteLine(BuildClass("CombatantAbility", ORK.Abilities.GetNames(false).ToList()));
writer.WriteLine(BuildClass("CombatantEquipmentPart", ORK.EquipmentParts.GetNames(false).ToList()));
// End of namespace ORKConstants
writer.WriteLine("}");
writer.WriteLine();
}
Debug.Log("Completed");
// Refresh
AssetDatabase.Refresh();
}
private static string BuildClass(string className, List<string> constantNames)
{
StringBuilder sb = new StringBuilder(" public static class ");
sb.AppendLine(className);
sb.AppendLine(" {");
try
{
for (int i = 0; i < constantNames.Count; i++)
{
sb.AppendLine(string.Format(" public const int {0} = {1};", MakeSafeForCode(constantNames[i].ToUpper()),
i));
}
}
catch (Exception ex)
{
Debug.LogError(string.Format("Could not generate class {0}: {1}", className, ex.Message));
}
finally
{
sb.AppendLine(" }");
sb.AppendLine();
}
return sb.ToString();
}
private static string MakeSafeForCode(string str)
{
str = Regex.Replace(str, "[^a-zA-Z0-9_]", "_", RegexOptions.Compiled);
if (char.IsDigit(str[0])) {
str = "_" + str;
}
return str;
}
}
}
#endif //UNITY_EDITOR
It looks like you're new here. If you want to get involved, click one of these buttons!