Paint in 3D Paint in 3D用於在游戲內和編輯器里繪製所有物體。所有功能已經過深度優化,在WebGL、移動端、VR 以及更多平臺用起來都非常好用! 它支持標準管線,以及 LWRP、HDRP 和 URP。通過使用GPU 加速,你的物體將以難以置信的速度被繪製。代碼還經過深度優化來防止 ...
Paint in 3D
Paint in 3D用於在游戲內和編輯器里繪製所有物體。所有功能已經過深度優化,在WebGL、移動端、VR 以及更多平臺用起來都非常好用!
它支持標準管線,以及 LWRP、HDRP 和 URP。通過使用GPU 加速,你的物體將以難以置信的速度被繪製。代碼還經過深度優化來防止GC,和將所有繪製操作一起批次完成。
跟貼圖系統不同,它是一個紋理繪製解決方案。這意味著你可以繪製你的物體上百萬次,還是無幀率丟失,讓你創作難以想象的游戲。
它在Unity應用商店上的售價是60美元,地址:https://assetstore.unity.com/packages/tools/painting/paint-in-3d-26286。
Photon
Photon中文翻譯為“光子”,為有著15年伺服器後端開發經驗的德國Exit Games公司開發的高效,穩定,可拓展的網路引擎。為目前世界上用戶最廣泛,支持游戲類型最多的專業網路引擎之一,也是Unity應用商店裡用戶評價最高的網路組件。
世界多個知名游戲公司和工作室選用Photon作為其產品的網路支持引擎,其中包括WB華納,Codemaster, 2K, Glu, 微軟游戲工作室,史克威爾艾尼克斯,百代南夢宮,SandBox,雨神電競等知名企業,也有許多工作室和新創企業正在瞭解和試用Photon之中。
它在Unity應用商店上有一個免費應用,地址:https://assetstore.unity.com/packages/tools/network/pun-2-free-119922。
當然,Photon需要註冊賬號、創建應用等操作才能使用,還不瞭解的同學可以去官方網站查閱相關資料。
溫馨提示:Photon的國外伺服器在國內使用比較卡,所以最好去中國官網申請國內的伺服器,申請地址:https://vibrantlink.com/chinacloudapply/。
下麵正式開始。
微信掃描二維碼關註後回覆「電子書」,獲取12本Java必讀技術書籍。創建工程
使用Unity Hub創建一個3D項目,然後分別引入Paint in 3D和Photon Unity Networking 2,如下圖:
溫馨提示:在引入Photon Unity Networking 2後,記得配置AppId。
創建簡易畫板
為了方便演示,我們創建一個Quad作為畫板,然後為其添加P3dPaintable、P3dMaterialCloner和P3dPaintableTexture組件,使用它們的預設配置即可,如下圖:
然後,創建一個空的GameObject命名為OneMorePaint
,然後向OneMorePaint
添加P3dPaintSphere組件,修改P3dPaintSphere組件的Color為紅色,其他配置保持預設不變,如下圖:
再向OneMorePaint
添加P3dHitScreen組件,勾選上P3dHitScreen組件的ConnectHits,其他配置保持預設不變,如下圖:
這時候,創建簡易畫板就做好了,運行以後就可以畫畫了,如下圖:
只不過,還是個單機版,我們加上實時線上功能。
微信掃描二維碼關註後回覆「電子書」,獲取12本Java必讀技術書籍。連接PUN2伺服器
創建一個C#腳本命名為Launcher
,再創建一個空的GameObject命名為LauncherGameObject
,把C#腳本Launcher
添加到LauncherGameObject
中。
編輯C#腳本Launcher
為如下內容:
using Photon.Pun;
using Photon.Realtime;
using UnityEngine;
namespace One.More
{
public class Launcher : MonoBehaviourPunCallbacks
{
#region Private Fields
/// <summary>
/// This client's version number. Users are separated from each other by gameVersion (which allows you to make breaking changes).
/// </summary>
string gameVersion = "1";
/// <summary>
/// Keep track of the current process. Since connection is asynchronous and is based on several callbacks from Photon,
/// we need to keep track of this to properly adjust the behavior when we receive call back by Photon.
/// Typically this is used for the OnConnectedToMaster() callback.
/// </summary>
bool isConnecting;
#endregion
void Start()
{
this.Connect();
}
#region MonoBehaviourPunCallbacks Callbacks
public override void OnConnectedToMaster()
{
Debug.Log("PUN Basics Tutorial/Launcher: OnConnectedToMaster() was called by PUN");
// we don't want to do anything if we are not attempting to join a room.
// this case where isConnecting is false is typically when you lost or quit the game, when this level is loaded, OnConnectedToMaster will be called, in that case
// we don't want to do anything.
if (isConnecting)
{
// #Critical: The first we try to do is to join a potential existing room. If there is, good, else, we'll be called back with OnJoinRandomFailed()
PhotonNetwork.JoinRandomRoom();
isConnecting = false;
}
}
public override void OnDisconnected(DisconnectCause cause)
{
Debug.LogWarningFormat("PUN Basics Tutorial/Launcher: OnDisconnected() was called by PUN with reason {0}", cause);
isConnecting = false;
}
public override void OnJoinRandomFailed(short returnCode, string message)
{
Debug.Log("PUN Basics Tutorial/Launcher:OnJoinRandomFailed() was called by PUN. No random room available, so we create one.\nCalling: PhotonNetwork.CreateRoom");
// #Critical: we failed to join a random room, maybe none exists or they are all full. No worries, we create a new room.
PhotonNetwork.CreateRoom(null, new RoomOptions());
}
public override void OnJoinedRoom()
{
Debug.Log("PUN Basics Tutorial/Launcher: OnJoinedRoom() called by PUN. Now this client is in a room.");
}
#endregion
#region Public Methods
/// <summary>
/// Start the connection process.
/// - If already connected, we attempt joining a random room
/// - if not yet connected, Connect this application instance to Photon Cloud Network
/// </summary>
public void Connect()
{
// we check if we are connected or not, we join if we are , else we initiate the connection to the server.
if (PhotonNetwork.IsConnected)
{
// #Critical we need at this point to attempt joining a Random Room. If it fails, we'll get notified in OnJoinRandomFailed() and we'll create one.
PhotonNetwork.JoinRandomRoom();
}
else
{
// #Critical, we must first and foremost connect to Photon Online Server.
isConnecting = PhotonNetwork.ConnectUsingSettings();
PhotonNetwork.GameVersion = gameVersion;
}
}
#endregion
}
}
這時候,就可以連接到連接PUN2伺服器了,運行以後我們可以看到如下日誌:
微信掃描二維碼關註後回覆「電子書」,獲取12本Java必讀技術書籍。實時線上同步
向之前創建的OneMorePaint
添加PhotonView組件,使用預設配置即可,如下圖:
創建一個C#腳本命名為OnlinePainting
,把C#腳本OnlinePainting
添加到OneMorePaint
中。
編輯C#腳本OnlinePainting
為如下內容:
using PaintIn3D;
using Photon.Pun;
using UnityEngine;
namespace One.More
{
public class OnlinePainting : MonoBehaviour, IHitPoint, IHitLine
{
private PhotonView photonView;
private P3dPaintSphere paintSphere;
void Start()
{
this.photonView = this.GetComponent<PhotonView>();
this.paintSphere = this.GetComponent<P3dPaintSphere>();
}
public void HandleHitPoint(bool preview, int priority, float pressure, int seed, Vector3 position, Quaternion rotation)
{
if (preview)
{
return;
}
if (this.photonView == null)
{
Debug.LogError("PhotonView is not found.");
return;
}
this.photonView.RPC("HandleHitPointRpc", RpcTarget.Others, preview, priority, pressure, seed, position, rotation);
}
public void HandleHitLine(bool preview, int priority, float pressure, int seed, Vector3 position, Vector3 endPosition, Quaternion rotation, bool clip)
{
if (preview)
{
return;
}
if (this.photonView == null)
{
Debug.LogError("PhotonView is not found.");
return;
}
this.photonView.RPC("HandleHitLinetRpc", RpcTarget.Others, preview, priority, pressure, seed, position, endPosition, rotation, clip);
}
[PunRPC]
public void HandleHitPointRpc(bool preview, int priority, float pressure, int seed, Vector3 position, Quaternion rotation)
{
if (this.paintSphere == null)
{
Debug.LogError("P3dPaintSphere is not found.");
return;
}
this.paintSphere.HandleHitPoint(preview, priority, pressure, seed, position, rotation);
}
[PunRPC]
public void HandleHitLinetRpc(bool preview, int priority, float pressure, int seed, Vector3 position, Vector3 endPosition, Quaternion rotation, bool clip)
{
if (this.paintSphere == null)
{
Debug.LogError("P3dPaintSphere is not found.");
return;
}
this.paintSphere.HandleHitLine(preview, priority, pressure, seed, position, endPosition, rotation, clip);
}
}
}
線上塗鴉畫板就製作完成了,我們看看運行效果怎麼樣?
運行效果
構建以後,同時啟動兩個客戶端,效果如下:
當然,這隻是簡單的線上塗鴉畫板,你還可以再此基礎上添加更豐富的功能,比如:修改畫筆顏色、修改畫筆大小等等。
微信掃描二維碼關註後回覆「電子書」,獲取12本Java必讀技術書籍。作者:萬貓學社最後,謝謝你這麼帥,還給我點贊和關註。
出處:http://www.cnblogs.com/heihaozi/
版權聲明:本文遵循 CC 4.0 BY-NC-SA 版權協議,轉載請附上原文出處鏈接和本聲明。
微信掃描二維碼,關註萬貓學社,回覆「電子書」,免費獲取12本Java必讀技術書籍。