環境同一
代碼如下
- 編輯unity界面
新建3Dobject panel 新建控制空對象Gameobject 新增小球預設體並添加基本剛體碰撞等
- 添加小球移動腳本
using UnityEngine;
using System.Collections;
public class move02 : MonoBehaviour
{
public void Left()
{
transform.Translate(Vector3.left * Time.deltaTime * 5);
}
public void Right()
{
transform.Translate(Vector3.right * Time.deltaTime * 5);
}
public void Forward()
{
transform.Translate(Vector3.forward * Time.deltaTime * 5);
}
public void Back()
{
transform.Translate(Vector3.back * Time.deltaTime * 5);
}
public void Up()
{
transform.Translate(Vector3.up * Time.deltaTime * 5);
}
public void Down()
{
transform.Translate(Vector3.down * Time.deltaTime * 5);
}
}
- gameobject中添加新建預設體小球腳本
using UnityEngine;
using System.Collections;
using System;
public class newSphere : MonoBehaviour
{
public GameObject boxBody; //障礙物
private static int i = 1;
private static int a = 0;
// Use this for initialization
private void Start()
{
}
// Update is called once per frame
private void Update()
{
if (Input.GetKeyDown(KeyCode.R))
{
AppearPosition();
}
}
private void AppearPosition()
{
Vector3 bodyAppearPosition = new Vector3(a, 0, 0);
Quaternion bodyAppearRotation = Quaternion.identity;
boxBody.name = "neweee" + i;
// Application.ExternalCall(boxBody.name);
Application.ExternalEval(boxBody.name); // 參數傳遞給winform,winform中有lable框時測試用的,此處可註釋,可看第一篇
i++;
a += 2;
Instantiate(boxBody, bodyAppearPosition, bodyAppearRotation);
}
}
- MainCamera中增加點擊或移動新建預設體小球腳本
拖拽移動
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class ModelDrage : MonoBehaviour
{
private Camera cam;//發射射線的攝像機
private GameObject go;//射線碰撞的物體
public static string btnName;//射線碰撞物體的名字
private Vector3 screenSpace;
private Vector3 offset;
private bool isDrage = false;
private void Start()
{
cam = Camera.main;
}
private void Update()
{
//整體初始位置
Ray ray = cam.ScreenPointToRay(Input.mousePosition);
//從攝像機發出到點擊座標的射線
RaycastHit hitInfo;
if (isDrage == false)
{
if (Physics.Raycast(ray, out hitInfo))
{
//劃出射線,只有在scene視圖中才能看到
Debug.DrawLine(ray.origin, hitInfo.point);
go = hitInfo.collider.gameObject;
//print(btnName);
screenSpace = cam.WorldToScreenPoint(go.transform.position);
offset = go.transform.position - cam.ScreenToWorldPoint(new Vector3(Input.mousePosition.x, Input.mousePosition.y, screenSpace.z));
//物體的名字
btnName = go.name;
//發送給winform
// Application.ExternalEval(btnName);
//組件的名字
}
else
{
btnName = null;
}
}
if (Input.GetMouseButton(0))
{
Vector3 currentScreenSpace = new Vector3(Input.mousePosition.x, Input.mousePosition.y, screenSpace.z);
Vector3 currentPosition = cam.ScreenToWorldPoint(currentScreenSpace) + offset;
if (btnName != null)
{
go.transform.position = currentPosition;
}
isDrage = true;
}
else
{
isDrage = false;
}
}
}
點擊獲取對象(傳遞對象名給winform需要控制的對象)
using UnityEngine;
using System.Collections;
public class ModelDrage02 : MonoBehaviour
{
// Use this for initialization
private void Start()
{
}
// Update is called once per frame
private void Update()
{
if (Input.GetMouseButtonDown(0))
{
Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
RaycastHit hit;
if (Physics.Raycast(ray, out hit))
{
GameObject go = hit.collider.gameObject; //獲得選中物體
string goName = go.name; //獲得選中物體的名字,使用hit.transform.name也可以
Application.ExternalEval(goName);
print(goName);
}
}
}
}
- webplayer發佈unity項目
- vs新建winform項目
- 設計winform面板如下
- 代碼編輯
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace UnityIntoWinformTest08
{
public partial class Form1 : Form
{
private static string abc;
private static string def;
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
axUnityWebPlayer1.SendMessage(eToNew(), "Left", null);
}
private void button2_Click(object sender, EventArgs e)
{
axUnityWebPlayer1.SendMessage(eToNew(), "Right", null);
}
private void button3_Click(object sender, EventArgs e)
{
axUnityWebPlayer1.SendMessage(eToNew(), "Forward", null);
}
private void button4_Click(object sender, EventArgs e)
{
axUnityWebPlayer1.SendMessage(eToNew(), "Back", null);
}
private void button5_Click(object sender, EventArgs e)
{
axUnityWebPlayer1.SendMessage(eToNew(), "Up", null);
}
private void button6_Click(object sender, EventArgs e)
{
axUnityWebPlayer1.SendMessage(eToNew(), "Down", null);
}
private void button7_Click(object sender, EventArgs e)
{
axUnityWebPlayer1.SendMessage("GameObject", "AppearPosition", null);
}
private void axUnityWebPlayer1_OnExternalCall(object sender, AxUnityWebPlayerAXLib._DUnityWebPlayerAXEvents_OnExternalCallEvent e)
{
label1.Text = e.value;
}
private void button8_Click(object sender, EventArgs e)
{
}
private void Form1_Load(object sender, EventArgs e)
{
}
private void label1_Click(object sender, EventArgs e)
{
}
private string eToNew()
{
/// abc = label1.Text.Substring(0, label1.Text.Length - 1) + "(Clone)";
abc = label1.Text.Substring(0, label1.Text.Length - 1);
return abc;
}
private string eToBtn()
{
def = label1.Text.Substring(0, label1.Text.Length - 1) + "(Clone)";
return def;
}
}
}
- unity src加上剛發佈的unity地址
- ok,運行界面 大功告成
new幾個預設體小球
點擊其中一個小球,label出現該小球名(在代碼裏預設的名字)
然後就可以單獨點擊winform 前後左右 移動了 ,當然, 直接在裏面拖拽也是可以的。