import UnityEngine class DialogSystem (MonoBehaviour): public gui_skin as GUISkin ## Insert desired skin here. lines as List current_line as int end_line as int showing as bool turned_page_last_frame as bool text_to_display as string speaker_name as string portrait_to_display as Texture ## Insert character portraits, naming them consistently with your conversations. (Case-sensitive.) public portraits as (Texture) def Start (): lines = [ {"speaker":"Harek","text":"Hi there! We're here to demonstrate a simple dialog system! (Hit Space to turn the page.)"}, {"speaker":"Harek","text":"To set this system up, create an empty GameObject in your project and attach this script to it, eg. by dragging and dropping."}, {"speaker":"Rollo","text":"Each conversation is a List of Hash (dictionary) objects. Each item in that List normally contains a field called 'text' which is what we say, and one called 'speaker' which identifies the name and portrait to show."}, {"speaker":"Hazel","text":"""For instance, a typical line looks like this: {"speaker":"Harek","text":"Hazel, you're way more awesome than me."},"""}, {"speaker":"Harek","text":"Haz... I mean, hey!"}, {"speaker":"Harek","text":"Word wrapping is important for long monologs like this one. You should specify a GUISkin other than the default. Do that by dragging one over to the public variable 'gui_skin' on this script's GameObject. We're using the free APT_GUI skin by Chrisblue, free on the Asset Store. (Thanks!) Click on the skin, in this case called APT, select 'Box', and check the checkbox marked 'Word Wrap'. You might want to set 'Alignment' to 'Upper Left' too."}, {"speaker":"Hazel","text":"Upper left alignment? I thought you were Chaotic, not Lawful."}, {"speaker":"Rollo","text":"If you want, you can specify a field called 'portrait' that overrides the 'speaker' field. That feature is meant for when you have several portraits for one character, and want to display the name 'Harek' but show the portrait called 'Harek_Cowering'."}, {"speaker":"Harek","text":"*grumbles* Anyway, the portraits are stored as a public variable of user-changeable length. Just drag and drop images into it. Then you can reference them with the 'portrait' variable, so long as you use exactly the same name. (Case-sensitive.)"}, {"speaker":"Rollo","text":"The OnGUI function draws the text box, speaker name, and portrait in particular locations and sizes, but of course you can change those to suit your needs."}, {"speaker":"Hazel","text":"We hope this little demo is helpful! The script (at least this version of it) is free as public domain. Not the portraits though; those are from Djinni, who takes commissions at djinni.livejournal.com."}, {"speaker":"Harek","text":"Feedback is welcome. This version of the demo has only one conversation built in, so there needs to be a way to pass Hash objects to the system. That much should be simple. Ideally it'd handle branching conversations, but that's a more advanced topic. We're done here for now! Hit Space and the dialog system will close."}, ] BeginConversation() def BeginConversation(): current_line = 0 end_line = len(lines) showing = true turned_page_last_frame = true Debug.Log(showing) Debug.Log(current_line) Debug.Log(end_line) def Update (): if showing: if Input.GetKeyDown(KeyCode.Space): current_line += 1 turned_page_last_frame = true if current_line == end_line: showing = false def GetPortrait(name): for p in portraits: if (p as Object).name == name: return p Debug.LogWarning("Portrait '" + name + "' is missing.") return null def OnGUI(): if showing: GUI.skin = gui_skin ## If the page advanced, set up the next page's text and picture. if turned_page_last_frame: line = lines[ current_line ] as Hash text_to_display = line["text"] as string ## If no speaker is given, no portrait is shown. speaker_name = (line["speaker"] or "") as string if not speaker_name: portrait = null else: ## Does the line specify a portrait that overrides the speaker's default? portrait = (line["portrait"] or "") as string portrait_to_display = GetPortrait(portrait or speaker_name) as Texture turned_page_last_frame = false ## Now, every frame: ## The text box. GUI.Box( Rect(150,0,700,300), text_to_display ) ## Speaker's name and portrait, each optional. if speaker_name: GUI.Box(Rect(0,0,150,75), speaker_name) if portrait_to_display: GUI.Box(Rect(0,75,150,150), portrait_to_display)