Added tools for pool creation
It using code generation and reflection
This commit is contained in:
parent
b963bd76f8
commit
f494fea030
@ -34,10 +34,10 @@ RectTransform:
|
|||||||
m_Father: {fileID: 0}
|
m_Father: {fileID: 0}
|
||||||
m_RootOrder: 0
|
m_RootOrder: 0
|
||||||
m_LocalEulerAnglesHint: {x: 0, y: 0, z: 0}
|
m_LocalEulerAnglesHint: {x: 0, y: 0, z: 0}
|
||||||
m_AnchorMin: {x: 0.5, y: 1}
|
m_AnchorMin: {x: 0, y: 1}
|
||||||
m_AnchorMax: {x: 0.5, y: 1}
|
m_AnchorMax: {x: 0, y: 1}
|
||||||
m_AnchoredPosition: {x: -670, y: -42.5}
|
m_AnchoredPosition: {x: 30, y: -25}
|
||||||
m_SizeDelta: {x: 100, y: 85}
|
m_SizeDelta: {x: 60, y: 50}
|
||||||
m_Pivot: {x: 0.5, y: 0.5}
|
m_Pivot: {x: 0.5, y: 0.5}
|
||||||
--- !u!222 &7612804925605912136
|
--- !u!222 &7612804925605912136
|
||||||
CanvasRenderer:
|
CanvasRenderer:
|
||||||
|
@ -475,11 +475,9 @@ MonoBehaviour:
|
|||||||
- {fileID: 4920621562230953387}
|
- {fileID: 4920621562230953387}
|
||||||
- {fileID: 1754320532}
|
- {fileID: 1754320532}
|
||||||
- {fileID: 220002226}
|
- {fileID: 220002226}
|
||||||
- {fileID: 552140506}
|
|
||||||
defaultViews:
|
defaultViews:
|
||||||
- {fileID: 678372253}
|
- {fileID: 678372253}
|
||||||
- {fileID: 1754320532}
|
- {fileID: 1754320532}
|
||||||
- {fileID: 552140506}
|
|
||||||
autoInitialize: 1
|
autoInitialize: 1
|
||||||
--- !u!4 &214096430
|
--- !u!4 &214096430
|
||||||
Transform:
|
Transform:
|
||||||
@ -778,7 +776,7 @@ GameObject:
|
|||||||
m_Icon: {fileID: 0}
|
m_Icon: {fileID: 0}
|
||||||
m_NavMeshLayer: 0
|
m_NavMeshLayer: 0
|
||||||
m_StaticEditorFlags: 0
|
m_StaticEditorFlags: 0
|
||||||
m_IsActive: 1
|
m_IsActive: 0
|
||||||
--- !u!224 &552140505
|
--- !u!224 &552140505
|
||||||
RectTransform:
|
RectTransform:
|
||||||
m_ObjectHideFlags: 0
|
m_ObjectHideFlags: 0
|
||||||
@ -2814,7 +2812,7 @@ PrefabInstance:
|
|||||||
objectReference: {fileID: 0}
|
objectReference: {fileID: 0}
|
||||||
- target: {fileID: 4889749525435056362, guid: 14c24fef9917a7445a67e6acdb9a0c0e, type: 3}
|
- target: {fileID: 4889749525435056362, guid: 14c24fef9917a7445a67e6acdb9a0c0e, type: 3}
|
||||||
propertyPath: m_AnchorMax.x
|
propertyPath: m_AnchorMax.x
|
||||||
value: 0
|
value: 1
|
||||||
objectReference: {fileID: 0}
|
objectReference: {fileID: 0}
|
||||||
- target: {fileID: 4889749525435056362, guid: 14c24fef9917a7445a67e6acdb9a0c0e, type: 3}
|
- target: {fileID: 4889749525435056362, guid: 14c24fef9917a7445a67e6acdb9a0c0e, type: 3}
|
||||||
propertyPath: m_AnchorMax.y
|
propertyPath: m_AnchorMax.y
|
||||||
@ -2826,15 +2824,15 @@ PrefabInstance:
|
|||||||
objectReference: {fileID: 0}
|
objectReference: {fileID: 0}
|
||||||
- target: {fileID: 4889749525435056362, guid: 14c24fef9917a7445a67e6acdb9a0c0e, type: 3}
|
- target: {fileID: 4889749525435056362, guid: 14c24fef9917a7445a67e6acdb9a0c0e, type: 3}
|
||||||
propertyPath: m_AnchorMin.y
|
propertyPath: m_AnchorMin.y
|
||||||
value: 1
|
value: 0
|
||||||
objectReference: {fileID: 0}
|
objectReference: {fileID: 0}
|
||||||
- target: {fileID: 4889749525435056362, guid: 14c24fef9917a7445a67e6acdb9a0c0e, type: 3}
|
- target: {fileID: 4889749525435056362, guid: 14c24fef9917a7445a67e6acdb9a0c0e, type: 3}
|
||||||
propertyPath: m_SizeDelta.x
|
propertyPath: m_SizeDelta.x
|
||||||
value: 100
|
value: 0.08999634
|
||||||
objectReference: {fileID: 0}
|
objectReference: {fileID: 0}
|
||||||
- target: {fileID: 4889749525435056362, guid: 14c24fef9917a7445a67e6acdb9a0c0e, type: 3}
|
- target: {fileID: 4889749525435056362, guid: 14c24fef9917a7445a67e6acdb9a0c0e, type: 3}
|
||||||
propertyPath: m_SizeDelta.y
|
propertyPath: m_SizeDelta.y
|
||||||
value: 85
|
value: 0
|
||||||
objectReference: {fileID: 0}
|
objectReference: {fileID: 0}
|
||||||
- target: {fileID: 4889749525435056362, guid: 14c24fef9917a7445a67e6acdb9a0c0e, type: 3}
|
- target: {fileID: 4889749525435056362, guid: 14c24fef9917a7445a67e6acdb9a0c0e, type: 3}
|
||||||
propertyPath: m_LocalPosition.x
|
propertyPath: m_LocalPosition.x
|
||||||
@ -2866,11 +2864,11 @@ PrefabInstance:
|
|||||||
objectReference: {fileID: 0}
|
objectReference: {fileID: 0}
|
||||||
- target: {fileID: 4889749525435056362, guid: 14c24fef9917a7445a67e6acdb9a0c0e, type: 3}
|
- target: {fileID: 4889749525435056362, guid: 14c24fef9917a7445a67e6acdb9a0c0e, type: 3}
|
||||||
propertyPath: m_AnchoredPosition.x
|
propertyPath: m_AnchoredPosition.x
|
||||||
value: 50
|
value: 0.049987793
|
||||||
objectReference: {fileID: 0}
|
objectReference: {fileID: 0}
|
||||||
- target: {fileID: 4889749525435056362, guid: 14c24fef9917a7445a67e6acdb9a0c0e, type: 3}
|
- target: {fileID: 4889749525435056362, guid: 14c24fef9917a7445a67e6acdb9a0c0e, type: 3}
|
||||||
propertyPath: m_AnchoredPosition.y
|
propertyPath: m_AnchoredPosition.y
|
||||||
value: -42.5
|
value: 0
|
||||||
objectReference: {fileID: 0}
|
objectReference: {fileID: 0}
|
||||||
- target: {fileID: 4889749525435056362, guid: 14c24fef9917a7445a67e6acdb9a0c0e, type: 3}
|
- target: {fileID: 4889749525435056362, guid: 14c24fef9917a7445a67e6acdb9a0c0e, type: 3}
|
||||||
propertyPath: m_LocalEulerAnglesHint.x
|
propertyPath: m_LocalEulerAnglesHint.x
|
||||||
@ -2890,11 +2888,11 @@ PrefabInstance:
|
|||||||
objectReference: {fileID: 0}
|
objectReference: {fileID: 0}
|
||||||
- target: {fileID: 4889749525435056363, guid: 14c24fef9917a7445a67e6acdb9a0c0e, type: 3}
|
- target: {fileID: 4889749525435056363, guid: 14c24fef9917a7445a67e6acdb9a0c0e, type: 3}
|
||||||
propertyPath: m_ChildScaleWidth
|
propertyPath: m_ChildScaleWidth
|
||||||
value: 1
|
value: 0
|
||||||
objectReference: {fileID: 0}
|
objectReference: {fileID: 0}
|
||||||
- target: {fileID: 4889749525435056363, guid: 14c24fef9917a7445a67e6acdb9a0c0e, type: 3}
|
- target: {fileID: 4889749525435056363, guid: 14c24fef9917a7445a67e6acdb9a0c0e, type: 3}
|
||||||
propertyPath: m_ChildScaleHeight
|
propertyPath: m_ChildScaleHeight
|
||||||
value: 1
|
value: 0
|
||||||
objectReference: {fileID: 0}
|
objectReference: {fileID: 0}
|
||||||
- target: {fileID: 4889749525435056363, guid: 14c24fef9917a7445a67e6acdb9a0c0e, type: 3}
|
- target: {fileID: 4889749525435056363, guid: 14c24fef9917a7445a67e6acdb9a0c0e, type: 3}
|
||||||
propertyPath: m_ChildControlWidth
|
propertyPath: m_ChildControlWidth
|
||||||
|
140
Assets/Scripts/Editor/PoolCreator.cs
Normal file
140
Assets/Scripts/Editor/PoolCreator.cs
Normal file
@ -0,0 +1,140 @@
|
|||||||
|
|
||||||
|
using UnityEditor;
|
||||||
|
using UnityEngine;
|
||||||
|
using System.CodeDom;
|
||||||
|
using System.CodeDom.Compiler;
|
||||||
|
using System.Reflection;
|
||||||
|
using System;
|
||||||
|
using System.IO;
|
||||||
|
|
||||||
|
public class PoolCodeGenerator
|
||||||
|
{
|
||||||
|
private CodeCompileUnit targetUnit;
|
||||||
|
private string targetClassName;
|
||||||
|
private string poolNameSpaceName;
|
||||||
|
private CodeTypeDeclaration targetClass;
|
||||||
|
private string outputFilePath;
|
||||||
|
|
||||||
|
public PoolCodeGenerator(string outputFilePath, string targetClassName, string poolNameSpaceName, string pooledObjectClassName)
|
||||||
|
{
|
||||||
|
|
||||||
|
this.targetClassName = targetClassName;
|
||||||
|
this.poolNameSpaceName = poolNameSpaceName;
|
||||||
|
this.outputFilePath = outputFilePath;
|
||||||
|
targetUnit = new CodeCompileUnit();
|
||||||
|
//TODO: MAKE NAMESPACE OPTIONAL
|
||||||
|
CodeNamespace poolNamespace = new CodeNamespace(poolNameSpaceName);
|
||||||
|
targetClass = new CodeTypeDeclaration();
|
||||||
|
targetClass.IsClass = true;
|
||||||
|
targetClass.Name = this.targetClassName;
|
||||||
|
targetClass.TypeAttributes =
|
||||||
|
TypeAttributes.Public| TypeAttributes.Sealed;
|
||||||
|
poolNamespace.Types.Add(targetClass);
|
||||||
|
targetUnit.Namespaces.Add(poolNamespace);
|
||||||
|
targetClass.BaseTypes.Add(new CodeTypeReference("BasePool",new CodeTypeReference(pooledObjectClassName)));//Add("BasePool");
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
public void GenerateCSharpCode()
|
||||||
|
{
|
||||||
|
CodeDomProvider provider = CodeDomProvider.CreateProvider("CSharp");
|
||||||
|
CodeGeneratorOptions options = new CodeGeneratorOptions();
|
||||||
|
options.BracingStyle = "C";
|
||||||
|
using (StreamWriter sourceWriter = new StreamWriter(outputFilePath,false))
|
||||||
|
{
|
||||||
|
provider.GenerateCodeFromCompileUnit(
|
||||||
|
targetUnit, sourceWriter, options);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
[CustomEditor(typeof(PoolingObject<>),true)]
|
||||||
|
public class PoolCreator : Editor
|
||||||
|
{
|
||||||
|
private string targetClassName;
|
||||||
|
private string poolNamespaceName;
|
||||||
|
private bool pendingToGeneration = false;
|
||||||
|
private int poolCapacity = 10;
|
||||||
|
|
||||||
|
private void OnEnable()
|
||||||
|
{
|
||||||
|
AssemblyReloadEvents.afterAssemblyReload += GeneratePoolPrefab;
|
||||||
|
}
|
||||||
|
private void OnDisable()
|
||||||
|
{
|
||||||
|
AssemblyReloadEvents.afterAssemblyReload -= GeneratePoolPrefab;
|
||||||
|
}
|
||||||
|
|
||||||
|
public override void OnInspectorGUI()
|
||||||
|
{
|
||||||
|
base.OnInspectorGUI();
|
||||||
|
|
||||||
|
EditorGUILayout.IntField("Pool Capacity: ",poolCapacity);
|
||||||
|
if (GUILayout.Button("Create Pool From This Object"))
|
||||||
|
{
|
||||||
|
string poolObjectName = target.GetType().Name;
|
||||||
|
string targetClassName = poolObjectName + "Pool";
|
||||||
|
string poolNamespaceName = "Pools";
|
||||||
|
this.targetClassName = targetClassName;
|
||||||
|
this.poolNamespaceName = poolNamespaceName;
|
||||||
|
PoolCodeGenerator generator = new PoolCodeGenerator($"{Application.dataPath}/Scripts/Road/Pools/{targetClassName}.cs", targetClassName, poolNamespaceName, poolObjectName); //$"{targetClassName}.cs"
|
||||||
|
var relativePath = $"Assets/Scripts/Road/Pools/{targetClassName}.cs";
|
||||||
|
generator.GenerateCSharpCode();
|
||||||
|
AssetDatabase.ImportAsset(relativePath);
|
||||||
|
AssetDatabase.Refresh();
|
||||||
|
EditorUtility.RequestScriptReload();
|
||||||
|
AssetDatabase.SaveAssets();
|
||||||
|
pendingToGeneration = true;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public void GeneratePoolPrefab()
|
||||||
|
{
|
||||||
|
if (!pendingToGeneration)
|
||||||
|
return;
|
||||||
|
GameObject poolingObject = new GameObject(targetClassName);
|
||||||
|
Type poolingObjectType = target.GetType();
|
||||||
|
Assembly assem = poolingObjectType.Assembly;
|
||||||
|
string poolName = $"{target.name}Pool";
|
||||||
|
Type poolType = assem.GetType($"{poolNamespaceName}.{targetClassName}");
|
||||||
|
//Type genericClass = typeof(BasePool<>);
|
||||||
|
//Type constructedClass = genericClass.MakeGenericType(poolingObjectType);
|
||||||
|
poolingObject.AddComponent(poolType);
|
||||||
|
poolingObject.name = poolName;
|
||||||
|
|
||||||
|
Type typeOfField = poolType;
|
||||||
|
FieldInfo fieldInfo = null;
|
||||||
|
while (fieldInfo == null && typeOfField != null)
|
||||||
|
{
|
||||||
|
fieldInfo = typeOfField.GetField("prefab", BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance);
|
||||||
|
typeOfField = typeOfField.BaseType;
|
||||||
|
}
|
||||||
|
if (fieldInfo == null) throw new ArgumentOutOfRangeException("Prefab", string.Format("Field {0} was not found in Type {1}", "prefab", typeOfField.FullName));
|
||||||
|
|
||||||
|
var poolingObjectComponent = poolingObject.GetComponent(poolType);
|
||||||
|
fieldInfo.SetValue(poolingObjectComponent, target);
|
||||||
|
Type capacityType = poolType;
|
||||||
|
//if (capacityType.GetProperty("Capacity", BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance) == null)
|
||||||
|
// throw new ArgumentOutOfRangeException("Capacity", string.Format("Property {0} was not found in Type {1}", "Capacity", obj.GetType().FullName));
|
||||||
|
//capacityType.InvokeMember("Capacity", BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.SetProperty | BindingFlags.Instance, null, obj, new object[] { 10 });
|
||||||
|
|
||||||
|
PropertyInfo propertyInfo = capacityType.BaseType.GetProperty("Capacity", BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance);
|
||||||
|
propertyInfo.SetValue(poolingObjectComponent, poolCapacity, BindingFlags.NonPublic | BindingFlags.Instance, null, null, null);
|
||||||
|
|
||||||
|
if (!Directory.Exists("Assets/Prefabs/Pools"))
|
||||||
|
AssetDatabase.CreateFolder("Assets/Prefabs", "Pools");
|
||||||
|
|
||||||
|
string localPath = "Assets/Prefabs/Pools/" + poolName + ".prefab";
|
||||||
|
|
||||||
|
localPath = AssetDatabase.GenerateUniqueAssetPath(localPath);
|
||||||
|
|
||||||
|
bool prefabSuccess;
|
||||||
|
PrefabUtility.SaveAsPrefabAssetAndConnect(poolingObject, localPath, InteractionMode.UserAction, out prefabSuccess);
|
||||||
|
|
||||||
|
if (prefabSuccess == true)
|
||||||
|
Debug.Log("Prefab was saved successfully");
|
||||||
|
else
|
||||||
|
Debug.Log("Prefab failed to save" + prefabSuccess);
|
||||||
|
Debug.Log("Done");
|
||||||
|
}
|
||||||
|
}
|
11
Assets/Scripts/Editor/PoolCreator.cs.meta
Normal file
11
Assets/Scripts/Editor/PoolCreator.cs.meta
Normal file
@ -0,0 +1,11 @@
|
|||||||
|
fileFormatVersion: 2
|
||||||
|
guid: 9ca19bcf0c7b9e64b94968ce8eeea0de
|
||||||
|
MonoImporter:
|
||||||
|
externalObjects: {}
|
||||||
|
serializedVersion: 2
|
||||||
|
defaultReferences: []
|
||||||
|
executionOrder: 0
|
||||||
|
icon: {instanceID: 0}
|
||||||
|
userData:
|
||||||
|
assetBundleName:
|
||||||
|
assetBundleVariant:
|
@ -1,6 +1,6 @@
|
|||||||
using UnityEngine;
|
using UnityEngine;
|
||||||
|
|
||||||
public class Turret : MonoBehaviour, IObstacle, IDamageDealer, IResettable
|
public class Turret : PoolingObject<Turret>, IObstacle, IDamageDealer, IResettable
|
||||||
{
|
{
|
||||||
public void DealDamage(IDamageable target, int amount)
|
public void DealDamage(IDamageable target, int amount)
|
||||||
{
|
{
|
||||||
|
@ -1,5 +1,4 @@
|
|||||||
using UnityEngine;
|
using UnityEngine;
|
||||||
|
|
||||||
public class BasePool<T> : MonoBehaviour where T : PoolingObject<T>
|
public class BasePool<T> : MonoBehaviour where T : PoolingObject<T>
|
||||||
{
|
{
|
||||||
[field: SerializeField] public int Capacity { get; private set; }
|
[field: SerializeField] public int Capacity { get; private set; }
|
||||||
|
@ -1,4 +1,4 @@
|
|||||||
using System;
|
using System;
|
||||||
using UnityEngine;
|
using UnityEngine;
|
||||||
|
|
||||||
public class ObstaclePool : BasePool<Obstacle>
|
public class ObstaclePool : BasePool<Obstacle>
|
||||||
|
18
Assets/Scripts/Road/Pools/TurretPool.cs
Normal file
18
Assets/Scripts/Road/Pools/TurretPool.cs
Normal file
@ -0,0 +1,18 @@
|
|||||||
|
//------------------------------------------------------------------------------
|
||||||
|
// <auto-generated>
|
||||||
|
// This code was generated by a tool.
|
||||||
|
// Runtime Version:4.0.30319.42000
|
||||||
|
//
|
||||||
|
// Changes to this file may cause incorrect behavior and will be lost if
|
||||||
|
// the code is regenerated.
|
||||||
|
// </auto-generated>
|
||||||
|
//------------------------------------------------------------------------------
|
||||||
|
|
||||||
|
namespace Pools
|
||||||
|
{
|
||||||
|
|
||||||
|
|
||||||
|
public sealed class TurretPool : BasePool<Turret>
|
||||||
|
{
|
||||||
|
}
|
||||||
|
}
|
11
Assets/Scripts/Road/Pools/TurretPool.cs.meta
Normal file
11
Assets/Scripts/Road/Pools/TurretPool.cs.meta
Normal file
@ -0,0 +1,11 @@
|
|||||||
|
fileFormatVersion: 2
|
||||||
|
guid: 15c0540313ca9dc42a7f526e0e8fc1a3
|
||||||
|
MonoImporter:
|
||||||
|
externalObjects: {}
|
||||||
|
serializedVersion: 2
|
||||||
|
defaultReferences: []
|
||||||
|
executionOrder: 0
|
||||||
|
icon: {instanceID: 0}
|
||||||
|
userData:
|
||||||
|
assetBundleName:
|
||||||
|
assetBundleVariant:
|
Loading…
x
Reference in New Issue
Block a user