I am trying to make an easy to use button inside the Unity Editor for Character and Item creation.
I would like to make the easy button for characters. Characters have Unique stats and skills, which is why they have to have their own scripts.
I want to be able to click “Create New Character” in the Unity editor and it do the following;
Prompt for a Name to use.
Create Empty Game Object named Name from whatever the user typed in.
Create a new C# Script named the same, and add it to the object.
-I want the generated script to have some pre-determined “Character Template” code in it.
Attach the new Script to the new empty game object, and attach a “Character Script” to it as well.
I think you may run into some problems with 3&4 because new scripts will need to be compiled before they can be executed. There is no way that I know of to compile at runtime, at least with unity alone. But let me ask you this, is that script really going to have code that you cannot write before-hand? If you CAN write it before hand, I think what you are looking for is a new INSTANCE of an already complied script (a “class” in particular), rather than a new c# script. If that’s correct, let us know, and we can provide more details.
Regarding 1&2, what do you have in place now? For example, do you have a button, text and editfield UI elements created in your scene yet? Do you have any code for the button yet?
Sorry that I wasn’t clear. That is an Editor button I wanted to make, not one for runtime.
I actually just have the character specific scripts inheriting from a base class script now, and it works fine. Each character has a massive amount of unique information, way more than would make sense to use as a new instance.
Also, at this point, I can simply copy and paste a “template” character scripts contents onto future character scripts. I was just hoping that I could get the Editor to create the new script for me, and place the “template” information in there.
I will have to spend some time learning Unity’s Window Editor to create the functionality I wanted. Thank you for your reply, but I will just make an “Easy Dev Tool Button” later.
Regarding an add-in for the editor: Ah, I see now, alas, I’m not the guy to help with that.
Each character has a massive amount of unique information, way more than would make sense to use as a new instance.
I must misunderstand: Why wouldn’t you want each instance of each character to have unique data, even if it’s a lot? If you meant NON-Unique data, that makes more sense, no need to have duplicated data in EACH instance- though in this case I would recommend you use STATIC members.
I use a base class CharacterScript, and I inherit from it on my CharacterSpecific scripts. In the game logic, I always call/use CharacterScript, but thanks to inheritance it automatically uses the information from the specific character I want. The idea is to have many many different characters. And, add more later. This way it works smoothly, and allows me to add more characters to the list later. If that makes sense.
If you use composition rather than inheritance, it will be easier to implement custom characters. For example, if your character is a spellcaster, you can add a Spellcaster script. If the character is a shapechanger, add a Shapechanger script. If Spellcaster and Shapechanger are separate subclasses of CharacterScript, it’s much harder to create a character that’s a spellcaster and a shapechanger.
For the “easy dev tool button”, I suggest using the [MenuItem] attribute to add a menu item that opens a New Character custom editor window:
using UnityEngine;
using UnityEditor;
using System.Collections;
class NewCharacterWindow : EditorWindow {
[MenuItem ("Window/New Character...")]
public static void ShowWindow () {
EditorWindow.GetWindow(typeof(NewCharacterWindow));
}
void OnGUI () {
if (GUILayout.Button("Create New Character")) CreateNewCharacter();
if (Selection.activeGameObject != null) {
characterName = EditorGUILayout.TextField("Name", characterName);
if (GUILayout.Button("Add Spellcaster")) {
Selection.activeGameObject.AddComponent<Spellcaster>();
}
}
}
void CreateNewCharacter() {
Selection.activeGameObject = new GameObject("New Character");
}
}
The code above in OnGUI() and CreateNewCharacter() is like pseudo-code. It won’t actually work the way it is right now. It’s only meant to give a general idea of the concept.