Problems with database
Im following the Burgzerg Arcade course to make RPG items, and im having the next problems:
Dont know what to do because in second image it should be showing the 2 fields (Name and sprite) and not what is showing, and in the first image is not saving to database.
Second Code should look like this one: http://prntscr.com/8y9rmv
Here are my codes:
using UnityEngine;
using System.Collections;
namespace Sakyadu.ItemSystem {
[System.Serializable]
public class ISTier : IISTier {
[SerializeField] string _name;
[SerializeField] Sprite _icon;
public ISTier(){
_name = "Common";
_icon = new Sprite();
}
public string Name{
get {return _name;}
set {_name = value;}
}
public Sprite Icon{
get {return _icon;}
set {_icon = value;}
}
}
}
using UnityEngine;
using System.Collections;
using System.Collections.Generic;
namespace Sakyadu.ItemSystem {
public class ISTierDatabase : ScriptableObject {
//[SerializeField]
public List<ISTier> database = new List<ISTier>();
}
}
The second one
using UnityEngine;
using System.Collections;
using System.Collections.Generic;
namespace Sakyadu.ItemSystem {
public class ISTierDatabase : ScriptableObject {
//[SerializeField]
public List<ISTier> database = new List<ISTier>();
}
}
The last one
using UnityEditor;
using UnityEngine;
using System.Collections;
namespace Sakyadu.ItemSystem.Editor {
public class ISTierDatabaseEditor : EditorWindow {
ISTierDatabase tierDatabase;
ISTier selectedItem;
Texture2D selectedTexture;
const int SPRITE_BUTTON_SIZE = 46;
const string FILE_NAME = @"sakyaduTierDatabase.asset";
const string DATABASE_FOLDER_NAME = @"Database";
const string DATABASE_FULL_PATH = @"Assets/" + DATABASE_FOLDER_NAME + "/" + FILE_NAME;
[MenuItem("SakyaduEditor/Database/Tier Editor %#i")]
public static void Init() {
ISTierDatabaseEditor window = EditorWindow.GetWindow<ISTierDatabaseEditor> ();
window.minSize = new Vector2 (200, 300);
window.title = "Tier DB";
window.Show ();
}
void OnEnable(){
tierDatabase = AssetDatabase.LoadAssetAtPath (DATABASE_FULL_PATH, typeof(ISTierDatabase)) as ISTierDatabase;
if (tierDatabase == null) {
if (!AssetDatabase.IsValidFolder("Assets/" + DATABASE_FOLDER_NAME))
AssetDatabase.CreateFolder("Assets", DATABASE_FOLDER_NAME);
tierDatabase = ScriptableObject.CreateInstance<ISTierDatabase>();
AssetDatabase.CreateAsset(tierDatabase,DATABASE_FULL_PATH);
AssetDatabase.SaveAssets();
AssetDatabase.Refresh();
}
selectedItem = new ISTier ();
}
void OnGUI(){
AddTierToDetabase();
}
void AddTierToDetabase(){
//Name
selectedItem.Name = EditorGUILayout.TextField("Name:", selectedItem.Name);
//Sprite
if (selectedItem.Icon)
selectedTexture = selectedItem.Icon.texture;
else
selectedTexture = null;
if (GUILayout.Button (selectedTexture, GUILayout.Width (SPRITE_BUTTON_SIZE), GUILayout.Height (SPRITE_BUTTON_SIZE))) {
int controlerID = EditorGUIUtility.GetControlID(FocusType.Passive);
EditorGUIUtility.ShowObjectPicker<Sprite>(null, true, null, controlerID);
}
string commandName = Event.current.commandName;
if (commandName == "ObjectSelectorUpdated") {
selectedItem.Icon = (Sprite)EditorGUIUtility.GetObjectPickerObject();
Repaint();
}
if (GUILayout.Button ("Save")) {
if (selectedItem == null)
return;
tierDatabase.database.Add(selectedItem);
selectedItem = new ISTier();
}
}
}
}
in second image it should be showing the 2 fields (Name and sprite)
Yes, it should give you a dropdown so you can drill into Name and Sprite. In fact, Serialized classes should never be null in Unity. $$anonymous$$aybe you forgot to save the ISTier.cs after adding [System.Serializable]?
Actually, what type is IISTier
? A plain interface? If it extends from UnityEngine.Object, then no, it won't work, because that will be a reference in Unity. Serializable classes should not extend from UnityEngine.Object.
I have my suspicions that you extend from ScriptableObject, $$anonymous$$onoBehaviour or some other Unity type.
Yeah, but it doesnt show the drop down menu. Here is the IISTier script:
using UnityEngine;
using System.Collections;
namespace Sakyadu.ItemSystem {
public class IISTier : IISObject {
string Name { get; set; }
Sprite Icon { get; set; }
}
}
And if needed, also here is the IISObject:
using UnityEngine;
using System.Collections;
namespace Sakyadu.ItemSystem {
public class IISObject : $$anonymous$$onoBehaviour {
/*Global*/
//Name
string ISName { get; set; }
//Pricing
int ISPrice { get; set; }
//Icon
Sprite ISIcon { get; set; }
//Tier
ISTier ISTier { get; set; }
//Weight
int ISWeight { get; set; }
/*Optional*/
//Equipable Flag
//Consumable Flag
//Special Flag
//Resistance
//Take Damage
}
}
Answer by Statement · Nov 03, 2015 at 12:05 AM
You can't create new MonoBehaviours using a constructor. MonoBehaviours are Components and must be added to GameObjects. Unity handle instantiation of Component types. See AddComponent, Component.
Documentation: Note that your code will never directly create a Component. Instead, you write script code, and attach the script to a GameObject. See Also: ScriptableObject as a way to create scripts that do not attach to any GameObject.
selectedItem = new ISTier();
This is invalid, because of the chain:
public class ISTier : IISTier
public class IISTier : IISObject
public class IISObject : MonoBehaviour
public class MonoBehaviour : Behaviour
public class Behaviour : Component
public class Component : Object < Docs say you cant create instances of this!
public class Object : System.Object
ISTier is-a Component and must be created as belonging to a valid GameObject.
If you want to support polymorphic serialization, consider extending from ScriptableObject instead, and using ScriptableObject.CreateInstance. If you don't need to serialize references polymorphically, do not extend from any UnityEngine.Object type.
The last screenshot you showed bear the hallmark of a class that does not extend from UnityEngine.Object - unless there had been a custom propertydrawer made for it, to make a Unity reference appear and behave as a plain old serialized class.
The options I see:
Drop the inheritance chain to MonoBehaviour and either...
Consider not extending from any UnityEngine type to make a Serializable object
Consider extending from ScriptableObject type to make a Referenceable object