Hello,
i try to do the following:
within Edit Mode i add a Script to an Gameobject and initalize the Scripts values. All happens via an EditorWindow-Class.
One of the values inside TheComponent is a generic list. In this list should be different subclasses of a single parent class. My idea was to create an interface, the parent class implements it, and then creating subclasses of the parent class, which finally would be stored in the list.
So much about the theory…
I spend some time testing now this stuff, but i couldn’t get it to work the way i want it, so the brief question would be: Is it actually possible to solve the described problem in Unitys Javascript?
Although i I found little to none documentation about how to use interfaces and generic lists in Javascript i refer to this site http://docs.unity3d.com/Documentation/Manual/MonoUpgradeDetails.html which promises that there seems to be a way to use them.
Without any further comment (it might be a little messed), here is one of my latest test-setups:
TheComponent.js:
#pragma strict
#pragma downcast
import System.Collections.Generic;
// test 1
var words:List.<String> = new List.<String>(); // init in Start()
var words2:List.<String> = new List.<String>(); // init in edit mode
// test 2
var books:List.<IBook> = new List.<IBook> (); // init in Start()
var books2:List.<IBook> = new List.<IBook> (); // init in edit mode
// test 3
var notebooks:List.<Notebook> = new List.<Notebook>(); //init in Start()
var notebooks2:List.<Notebook> = new List.<Notebook>(); //init in edit mode
function Start () {
/// test 1
Debug.Log("Test 1:");
words.Add("a");
words.Add("b");
words.Add("c");
for (word in words) {
Debug.Log(word);
}
Debug.Log("words2:List.<String> initalized in Edit Mode:");
for (word in words2)
Debug.Log(word);
/// test 2
Debug.Log("Test 2:");
books.Add(new Notebook("Arschus Aspire"));
books.Add(new Rulebook("WH40k Core Rules"));
books.Add(new Handbook("XYZ Handbook"));
books.Add(new SubNotebook("ABC SubNotebook"));
for (book in books) {
Debug.Log(book);
book.Info();
}
Debug.Log("Book list, initalized in edit mode:");
Debug.Log(books2.Count);
for (book in books2) {
Debug.Log(book);
book.Info();
}
/// test 3
Debug.Log("Test 3:");
notebooks.Add(new SubNotebook("XYZ subnotebook"));
for (notebook in notebooks) {
Debug.Log(notebook);
notebook.Info();
}
Debug.Log(notebooks2.Count);
for (notebook in notebooks2) {
Debug.Log(notebook);
notebook.Info();
}
}
interface IBook {
function Info();
}
class Book implements IBook {
function Info() {
Debug.Log("Book.Info()");
}
}
class Handbook implements IBook {
var name:String;
function Handbook(name:String) {
this.name = name;
}
function Info() {
Debug.Log("This is a handbook with the name: " + name);
}
}
class Rulebook implements IBook {
var name:String;
function Rulebook(name:String) {
this.name = name;
}
function Info() {
Debug.Log("This is a rulebook with the name: " + name);
}
}
class Notebook implements IBook {
var name:String;
function Notebook(name:String) {
this.name = name;
}
function Info() {
Debug.Log("This is a Notebook with the name: " + name);
}
}
class SubNotebook extends Notebook {
function SubNotebook(name:String) {
super(name);
}
function Info() {
Debug.Log("This is a SubNotebook with the name: " + name);
}
}
BookEditorUI.js (in Editor Folder):
#pragma strict
class BookEditorUI extends EditorWindow {
private var hs:TheComponent;
@MenuItem ("Window/Test/BookEditorUI")
static function Init () {
EditorWindow.GetWindow(BookEditorUI);
}
function TestInit() {
// test 1
Debug.Log("Test 1 (Edit Mode):");
hs.words2.Add("aa");
hs.words2.Add("bb");
for (word in hs.words2)
Debug.Log(word);
// test 2
Debug.Log("Test 2 (Edit Mode):");
hs.books2.Add(new Notebook("Arschus Aspire"));
hs.books2.Add(new Rulebook("WH40k Core Rules"));
hs.books2.Add(new Handbook("XYZ Handbook"));
hs.books2.Add(new SubNotebook("ABC SubNotebook"));
for (book in hs.books2) {
Debug.Log(book);
book.Info();
}
// test 3
Debug.Log("Test 3 (Edit Mode):");
hs.notebooks2.Add(new SubNotebook("Sub-Edit-Test"));
for (notebook in hs.notebooks2) {
Debug.Log(notebook);
notebook.Info();
}
}
function OnGUI () {
var obj = Selection.activeGameObject;
if (GUILayout.Button("Add Component")) {
if (obj.GetComponent(TheComponent) == null)
hs = obj.AddComponent(TheComponent);
}
GUI.enabled = (obj.GetComponent(TheComponent) != null) ? true : false;
if (GUILayout.Button("Init Component"))
TestInit();
GUI.enabled = true;
}
}