/* * SPDX-License-Identifier: AGPL-3.0-or-later * Copyright (C) 2025 Sergej Görzen * This file is part of OmiLAXR. */ using System.Linq; using UnityEngine; namespace OmiLAXR { /// /// Represents a team that can contain multiple actors as members. /// Teams can be used to group actors together and manage their relationships. /// [AddComponentMenu("OmiLAXR / Actors / Team")] public class Team : PipelineComponent { /// /// The display name of the team. /// public string teamName = "Team"; /// /// The contact email address for the team. /// public string teamEmail = "team@omilaxr.dev"; /// /// Returns the current array of team members. /// /// Array of Actor components that are members of this team. public Actor[] GetMembers() => _members; /// /// Private cache of team members. /// private Actor[] _members; /// /// Initializes the team by updating the member list when the component starts. /// private void Start() { UpdateMemberList(); } /// /// Refreshes the internal list of team members by finding all Actors in the scene /// that belong to this team. /// public void UpdateMemberList() { #if UNITY_2022_3_OR_NEWER // Uses the newer, more efficient FindObjectsByType method in Unity 2022.3+ _members = FindObjectsByType(FindObjectsSortMode.InstanceID) .Where(actor => Equals(actor.team)).ToArray(); #else // Falls back to FindObjectsOfType for older Unity versions _members = FindObjectsOfType() .Where(actor => Equals(actor.team)).ToArray(); #endif } /// /// Adds a single actor to the team and updates the member list. /// /// The Actor component to add to the team. public void AddMember(Actor actor) { actor.team = this; UpdateMemberList(); } /// /// Adds multiple actors to the team and updates the member list. /// /// Array of Actor components to add to the team. public void AddMembers(params Actor[] actors) { foreach (var a in actors) a.team = this; UpdateMemberList(); } } }