In certain cases during the game, something that only happens in response to a click and has a deep impact on the scene - this means I can also use some processor intensive functions - to a point (I’m developing for mobile) causes lots of components contained in the gameobjects to trigger and react accordingly.
This is actually a Time-switch mechanic. So, when the player taps on a button, the age changes and all components that use
using UnityEngine;
using System.Collections;
using AdvancedInspector;
[ExecuteInEditMode]
public class AgeSpecificAttribute<T> : MonoBehaviour {
public T valuePresent;
public T valueAlternate;
[ReadOnly]
public T value;
as a base, should be triggered.
On another component, the AgeManager PersistentSingleton, I have this piece of non working code:
Now, my intent should be clear: without having to register tons of event handlers, I simply want to get all the components based on AgeSpecificAttribute, and iterate through the array, broadcasting the AgeChanged message.
I’d also would like to avoid sending actual messages to the whole scene hierarchy.
What do you suggest?
I don’t use generic classes too often so I’m a little unexperienced in solving propably small problems like this.
Could you please suggest me the right syntax or alternative way to accomplish this?
public class GenericBase<T> : MonoBehaviour
{
public T SomeValue;
}
public class GenericInt : GenericBase<int>
{
}
public class zTestScript03 : MonoBehaviour {
void Start () {
var objs = GameObject.FindObjectsOfType<GenericBase<int>>();
foreach(var obj in objs)
{
Debug.Log(obj.name);
}
}
}
That is of course what you mean to ask is if you could get ALL classes that happen to inherit from AgeSpecificAttribute and not AgeSpecificAttribute.
In which case… no there is no way to do that because AgeSpecificAttribute isn’t actually a class. It’s the template for a class that accepts whatever type T you put into it. What happens when you go an instantiate a ‘SomeClass’ it takes the definition ‘SomeClass’ and generates a class at runtime that has ‘SomeType’ in all the places ‘T’ is found.
A way to get around this is to have a base class of that as well, and that you can get:
public class AgeSpecificAttribute : MonoBehaviour
{
}
public class AgeSpecificAttribute<T> : AgeSpecificAttribute
{
public T SomeValue;
}
public class AgeSpecificAttributeInt : AgeSpecificAttribute<int>
{
}
public class zTestScript03 : MonoBehaviour {
void Start () {
var objs = GameObject.FindObjectsOfType<AgeSpecificAttribute>();
foreach(var obj in objs)
{
Debug.Log(obj.name);
}
}
}
Easy and clean to implement, I like the idea of the common ancestor class. Which I may also be reusing for other classes who need to be “Agemanaged”. It should probably be an interface, but I’ll go for the class