unity3d的playmaker插件使用教程,九、playmaker和脚本

这里简单演示,playmaker如何调用脚本中的方法,以及脚本如何调用playmaker。


先在场景里添加一个盒子



在cube上

添加ForceButton、Rotate状态,

添加spin_cube事件



在ForceButton状态下添加GUI Button动作



新建脚本 RandomRotCube,并将脚本添加为Cube的组件

using UnityEngine;
using System.Collections;

public class RandomRotCube : MonoBehaviour {

	private float  rotAmount;

	public void RandRotCube(){
		rotAmount = Random.Range (-40, 40);
		print (rotAmount);

		transform.rotation = Quaternion.Euler (0f, rotAmount, 0f);
	}
}




在Roatate状态下添加Send Message动作。



设置send Message中的方法名称是脚本中需要调用的方法名。



运行预览,点击按钮后,方块随机转一个角度。



要传递参数也很方便,添加一个方法,带参数的,

using UnityEngine;
using System.Collections;

public class RandomRotCube : MonoBehaviour {

	private float  rotAmount;

	public void RandRotCube(){
		rotAmount = Random.Range (-40, 40);
		print (rotAmount);

		transform.rotation = Quaternion.Euler (0f, rotAmount, 0f);
	}

	public void ApplyForce(float userForce){
		GetComponent<Rigidbody> ().AddForce (0f, userForce, 0f);
	}
}


给方块添加刚体组件,在Rotate状态下再添加一个Send Message,并设置方法名称和参数


运行预览,点击按钮后,方块不但会转还会跳起来。



接下来是在脚本里调用playmaker。

在场景里添加一个带GUI Text的游戏对象,在其下面添加Listener、Random状态和SetNumber事件



添加String类型参数 currentNumber和Int类型参数radInt



在Random状态下添加动作,Random Int、Convert Int To String和Set GUI Text,随机出一个整数以后再GUI Text上显示。



修改GameObejct的名称



新建一个脚本

using UnityEngine;
using System.Collections;

public class CallFSM : MonoBehaviour {

	private PlayMakerFSM fsm;

	void Start(){
		fsm = GameObject.Find ("GUITextShow").GetComponent<PlayMakerFSM> ();
	}

	void OnGUI(){
		if (GUI.Button (new Rect (100, 100, 100, 100), "script call")) {
			fsm.Fsm.Event ("SetNumber");
		}
	}
}


将脚本添加为组件



运行预览,点击script call按钮,会随机显示整数。


  • 8
    点赞
  • 39
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值