So I am creating a game, which is a Novel game. I want to add a functionality where my friend can add a new dialogue on a character game object. To do that, I already created a main script that enqueue and dequeue all of what is written on the inspector. Two more scripts, one script is a class for creating a new properties on the inspector where my friend can write, the other script functionality is to patch it on the inspector itself. I decided to add a patcher to customize unity editor to add a button. All what is left is a function to add another class where in my friend can write another character name and sentences.
This is what it looks like on Unity Inspector:
Please Help.
DialogueManager.cs
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class DialogueManager : MonoBehaviour
{
private Queue<string> sentences;
public Text WrapperName;
public Text WrapperContent;
void Start()
{
sentences = new Queue<string>();
}
public void StartDialogue (Dialogue dialogue)
{
WrapperName.text = dialogue.name;
sentences.Clear();
foreach (string sentence in dialogue.sentences)
{
sentences.Enqueue(sentence);
}
DisplayNextSentence();
}
public void DisplayNextSentence()
{
if(sentences.Count == 0)
{
EndDialogue();
return;
}
string sentence = sentences.Dequeue();
WrapperContent.text = sentence;
}
public void EndDialogue()
{
Debug.Log("dialogue Ended..");
}
}
Dialogue.cs
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
[System.Serializable]
public class Dialogue
{
public string name;
[TextArea(3, 10)]
public string[] sentences;
}
StoryElement.cs
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class StoryElement : MonoBehaviour
{
public Dialogue dialogue;
public void TriggerDialogue()
{
FindObjectOfType<DialogueManager>().StartDialogue(dialogue);
}
}
elementscriptpatcher.cs
using System.Collections;
using UnityEditor;
using UnityEngine;
[CustomEditor(typeof(StoryElement))]
public class elementscriptpatcher : Editor
{
public override void OnInspectorGUI()
{
DrawDefaultInspector();
if(GUILayout.Button("Add another script"))
{
// I need to write a function for appending a class Dialogue on Dialogue.cs which was initialized on the StoryElement.
}
}
}
you can use GameObject.AddComponent.
if(GUILayout.Button("Add another script"))
{
gameObject.AddComponent<Dialogue>();
}
EDIT : As #derHugo said in comments, we can't add a class that's base is not Monobehaviour. So you will require an Access Class to access Dialogue.cs.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class AccessClass : MonoBehaviour {
public Dialogue mClassObject;
// Start is called before the first frame update
void Start()
{
}
// Update is called once per frame
void Update()
{
}
}
Use Ink for Unity, and author the dialogue as text files.
Related
haven't posted on here before but I have been trying for a while to create a game and would like a death/game over sort of scene to appear when the player loses all 3 of their lives. I have a functioning game manager and my player can lose lives (they have 3). This is all being done in unity games and is 2d (idk if that helps). I currently have other stuff in my scene loader script that works fine so I will post the whole thing but I am having issues with the bottom most code!
Thank you!
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.SceneManagement;
public class SceneLoader : MonoBehaviour
{
public string scenename;
public GameManager GM;
private void OnTriggerEnter2D(Collider2D collision)
{
if(collision.tag == "Player")
{
SceneManager.LoadScene(scenename);
}
}
private void Deathscene()
{
if(GM.LifeTotal == 0)
{
SceneManager.LoadScene(Bob);
}
}
}
Gamemanager script
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class GameManager : MonoBehaviour
{
public int PotionsCollected = 0;
public int LifeTotal = 3;
public Text PotionsOutput;
public Text LifeOutput;
void Update()
{
PotionsOutput.text = "Potions: " + PotionsCollected;
LifeOutput.text = "Life: " + LifeTotal;
}
public void CollectPotion()
{
PotionsCollected++;
}
public void UsePotion()
{
PotionsCollected--;
}
public void LoseLife()
{
LifeTotal--;
}
}
What you can do is from your Unity Editor go to File->Build Settings and then drag and drop inside the active scenes window your death scene.
Then an index will be generated on the right side of the window and you can use that index to load the scene. like this:
SceneManager.LoadScene("Use your generated index");
NOTE: Use your index as a number and not as a string.
UPDATED Solution:
public void LoseLife()
{
LifeTotal--;
if(LifeTotal <= 0)
{
SceneManager.LoadScene("Use your generated index");
}
}
I supposse LoseLife() it's called when the enemy attacks your player.
A simple question: I'm trying to modify UI text (TextMeshPro if that makes any difference) via C# script. I am using the following code:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class Coins : MonoBehaviour
{
public Text coins;
void Start()
{
coins = GetComponent<Text>();
}
void Update()
{
coins.text = "text";
}
}
I've done a similar thing in Unity 2018 (I'm currently using Unity 2020.2) and it worked there.
For some reason it doesn't work here though. I would appreciate any help.
Changing text in TMP is practicly the same, but you need to add "using TMPro;" and also change variable type. The code should look like this:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using TMPro;
public class Coins : MonoBehaviour
{
public TMP_Text coins;
void Start()
{
coins = GetComponent<TextMeshProUGUI>();
}
void Update()
{
coins.text = "text";
}
}
To modify TextMeshPro components, you have to use TMP_Text class.
public class Coins : MonoBehaviour
{
public TMP_Text coins;
void Start()
{
coins = GetComponent<TMP_Text>();
}
void Update()
{
coins.text = "text"; //or coins.SetText(“text”);
}
}
You need to reference the tmp text component instead of the normal Unity text one:
Instead of GetComponent<Text>(); do GetComponent<TextMeshProUGUI>();
Of course don’t forget:
using TMPro;
on top of your code.
I'm trying to get the title/description of a playing card to change by creating a card class that holds the information:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
[CreateAssetMenu (menuName = "Card")]
public class Card : ScriptableObject
{
public string cardName;
public Sprite art;
public string cardDetail;
}
Then load it with another script:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class CardViz : MonoBehaviour
{
public Text title;
public Text detail;
public Image art;
public Card card;
private void start()
{
LoadCard(card);
}
public void LoadCard(Card c)
{
if (c == null)
{
return;
}
card = c;
title.text = c.cardName;
detail.text = c.cardDetail;
art.sprite = c.art;
}
}
I created prefab with the basic layout of a card. Then I created a new asset value in unity for a card and given it a name and detail. Then assigned it to the public valuable Card under CardViz along with the corresponding title, detail and image variable to create a new prefab but none of the text change when I drag the newly made prefab into the hierarchy. Any clue as to what I'm doing wrong here?
A small typo. Your start method needs to have a capital s.
private void Start()
{
LoadCard(card);
}
Is it possible to use UnityEvents and/or UnityActions in scriptable objects?
My attempt looks like this:
using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.Events;
[CreateAssetMenu(menuName ="Dog", fileName ="New Dog")]
public class Dog : ScriptableObject
{
public string Id;
public string Text;
public UnityEvent WhenFinished;
public GameObject Go;
}
Is it possible to make a scriptable object hold a reference to a GO in the hierarchy or have an event point to a listener in the hierarchy. Is there anyway a scriptable object can communicate with the logic in the hierarchy?
Yes and no.
You can get an SO to GO reference and the reverse, but only during runtime.
When outside runtime, you can only get a SO to GO reference, an SO created in the editor outside of runtime can't reference any game object (it wouldn't know from which scene to get the GO).
Following is the code showing how to achieve this.
Scriptable Object Code
using System;
using UnityEngine;
[CreateAssetMenu(menuName = "Dog", fileName = "New Dog")]
public class Dog : ScriptableObject {
public GameObject Go;
public static event Action<string> ScriptableToGameobjectEvent;
private void Awake() {
EventScript.GameobjectToScriptableEvent += InstanceCreated;
if (Application.isPlaying) {
ScriptableToGameobjectEvent("Dog Created");
}
}
private void InstanceCreated(GameObject go) {
Go = go;
Debug.Log(Go.name);
}
}
Game Object Code
using UnityEngine;
using System;
public class EventScript : MonoBehaviour {
public static event Action<GameObject> GameobjectToScriptableEvent;
public ScriptableObject myScriptable;
private void Awake() {
Dog.ScriptableToGameobjectEvent += DogCreated;
}
private void Start() {
myScriptable = ScriptableObject.CreateInstance<Dog>();
}
private void DogCreated(string str) {
Debug.Log(str);
GameobjectToScriptableEvent(gameObject);
}
}
I have had no previous issues creating custom editors, however with this one it seems to be informing me "Multi-object editing not supported" the code for my script is below as well as the script for my custom editor. Is there something that I'm missing?
using UnityEngine;
using UnityEngine.UI;
using System.Collections;
using System.Collections.Generic;
[AddComponentMenu("Biophase Games/UI/Togglr")]
[System.Serializable]
public class Togglr : MonoBehaviour {
public List<Toggler> toggles;
public ColorBlock onColors;
public ColorBlock offColors;
public string value;
void Update() {
foreach(Toggler t in toggles) {
if (t.toggle.isOn == true) {
value = t.text;
t.toggle.colors = onColors;
} else {
t.toggle.colors = offColors;
}
}
}
}
[System.Serializable]
public class Toggler {
public Toggle toggle;
public string text;
}
and the CustomEditor script
using UnityEngine;
using UnityEngine.UI;
using UnityEditor;
using System.Collections;
using System.Collections.Generic;
[CustomEditor(typeof(Togglr))]
[CanEditMultipleObjects]
public class TogglrEditor : Editor {
SerializedProperty onColors;
SerializedProperty offColors;
SerializedProperty toggles;
void OnEnable() {
onColors = serializedObject.FindProperty ("onColors");
offColors = serializedObject.FindProperty ("offColors");
toggles = serializedObject.FindProperty ("toggles");
}
public override void OnInspectorGUI() {
serializedObject.Update ();
EditorGUILayout.PropertyField (toggles, true);
EditorGUILayout.PropertyField (onColors);
EditorGUILayout.PropertyField (offColors);
serializedObject.ApplyModifiedProperties ();
}
}
On an attempt to solve my issue, I moved the class Toggler into it's own file which subsequently solved the issue I was having.