Skip to content

Instantly share code, notes, and snippets.

@toqueteos
Last active December 28, 2015 19:39
Show Gist options
  • Select an option

  • Save toqueteos/3770f2cea1ad48e6fd02 to your computer and use it in GitHub Desktop.

Select an option

Save toqueteos/3770f2cea1ad48e6fd02 to your computer and use it in GitHub Desktop.
TNAutoDisableOthers
//---------------------------------------------
// Tasharen Network
// Copyright © 2012-2013 Tasharen Entertainment
// Copyright © 2013 toqueteos at gmail dot com
//---------------------------------------------
using TNet;
using UnityEngine;
using System.Reflection;
using StdGeneric = System.Collections.Generic;
/// <summary>
/// This script makes it really easy to disable components on all other connected clients
/// whenever they connect.
/// </summary>
public class TNAutoDisableOthers : TNBehaviour {
[HideInInspector]
public StdGeneric.List<Component>
disabled = new StdGeneric.List<Component>();
void OnEnable() {
foreach (Component c in disabled) {
var t = c.GetType();
if (!tno.isMine) {
t.GetProperty("enabled").SetValue(c, false, null);
}
}
}
}
//---------------------------------------------
// Tasharen Network
// Copyright © 2012-2013 Tasharen Entertainment
// Copyright © 2013 toqueteos at gmail dot com
//---------------------------------------------
using TNet;
using UnityEngine;
using UnityEditor;
using System.Reflection;
using StdGeneric = System.Collections.Generic;
/// <summary>
/// Inspector class used to view and edit TNAutoDisableOthers.
/// </summary>
[CustomEditor(typeof(TNAutoDisableOthers))]
public class TNAutoDisableOthersInspector : Editor {
private StdGeneric.List<Component> components;
public override void OnInspectorGUI() {
TNAutoDisableOthers self = target as TNAutoDisableOthers;
components = GetComponents(self);
if (components.Count == 0) {
GUI.backgroundColor = Color.red;
GUILayout.Button("No Components Available.");
return;
}
for (int i = 0; i < self.disabled.Count; i++) {
DrawTarget(self, i, self.disabled[i]);
}
GUI.backgroundColor = Color.green;
if (GUILayout.Button("Add a New Component to Disable")) {
self.disabled.Add(components[0]);
EditorUtility.SetDirty(self);
}
GUI.backgroundColor = Color.white;
}
void DrawTarget(TNAutoDisableOthers self, int index, Component c) {
GUILayout.BeginHorizontal();
GUI.backgroundColor = Color.red;
bool delete = GUILayout.Button("X", GUILayout.Width(24f));
GUI.backgroundColor = Color.white;
int ci = components.IndexOf(c);
int pos = EditorGUILayout.Popup(ci, GetComponentNames(components));
if (delete) {
self.disabled.RemoveAt(index);
EditorUtility.SetDirty(self);
}
if (pos != ci) {
self.disabled[index] = components[pos];
EditorUtility.SetDirty(self);
}
GUILayout.EndHorizontal();
}
static StdGeneric.List<Component> GetComponents(TNAutoDisableOthers self) {
// Needs the boolean arg to work within "Project tab".
Component[] comps = self.GetComponentsInChildren<Component>(true);
var list = new StdGeneric.List<Component>();
foreach (Component c in comps) {
var t = c.GetType();
var hasEnabled = t.GetProperty("enabled");
if (hasEnabled != null && c != self && hasEnabled.CanRead && hasEnabled.CanWrite) {
list.Add(c);
}
}
return list;
}
static string[] GetComponentNames(StdGeneric.List<Component> types) {
var names = new StdGeneric.List<string>();
foreach (Component c in types) {
names.Add(c.GetType().Name);
}
return names.ToArray();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment