1、导入fungus插件
下载好资源包之后,直接将其拖拽到Assets下即可
导入成功的标志是出现Tools
配置对话:Tools-Create-Flowchart
接着Flowchart-open Flowchart Window-npc1(你们的应该是NEW...)
先将Execute On Event设置为None
双击右中下方加号:Narrative-Say,即可编辑对话
2、场景说明
(1)3D简易场景
一个Panel,Capsule(作为Player) ,Cube(作为npc)
配置:player:Collider不勾选Is Trigger,Rigidbody
player的代码:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class player : MonoBehaviour
{
public float movespeed = 2f;
private float hor, ver;
void Update()
{
hor = Input.GetAxis("Horizontal");
ver = Input.GetAxis("Vertical");
Vector3 dir = new Vector3(-ver,0 ,hor );//具体按自己场景更改
//前后移动
transform.position += dir * Time.deltaTime * movespeed;
}
}
npc配置:
记得挂载脚本的chatname是你对话的名称我的是npc1
npc代码:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using Fungus;
public class npc : MonoBehaviour
{
public string chatname;
private bool canchat = false;
private void Update()
{
say();
}
void say()
{
if (canchat)
{
//对话
Flowchart flowchart = GameObject.Find("Flowchart").GetComponent<Flowchart>();
//对话是否存在
if (flowchart.HasBlock(chatname))
{
flowchart.ExecuteBlock(chatname);
}
}
}
private void OnTriggerEnter(Collider other)
{
if (other.CompareTag("Player"))
{
canchat = true;
}
}
private void OnTriggerExit(Collider other)
{
if (other.CompareTag("Player"))
{
canchat = false;
}
}
}
(2)2D场景,和3D大差不大
player
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class player : MonoBehaviour
{
public float movespeed = 2f;
private float hor, ver;
void Update()
{
hor = Input.GetAxis("Horizontal");
ver = Input.GetAxis("Vertical");
Vector3 dir = new Vector3(hor, ver, 0);
//前后移动
transform.position += dir * Time.deltaTime * movespeed;
}
}
npc1
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using Fungus;
public class npc : MonoBehaviour
{
public string chatname;
private bool canchat = false;
void OnTriggerEnter2D(Collider2D other)
{
if (other.CompareTag("Player"))
{
canchat = true;
StartDialogue();
}
}
void OnTriggerExit2D(Collider2D other)
{
if (other.CompareTag("Player"))
{
canchat = false;
}
}
void StartDialogue()
{
if (canchat)
{
// 获取Flowchart组件来开始对话
Flowchart flowchart = GameObject.Find("Flowchart").GetComponent<Flowchart>();
// 检查对话块是否存在
if (flowchart.HasBlock(chatname))
{
flowchart.ExecuteBlock(chatname);
}
}
}
}
以上有借鉴的代码,请勿商用哦