Normcore Multiplayer 笔记

已更新:3月15日

OVR Lipsync from


官方给的是个同步颜色的案例:

Normcore的model有点容易和模型搞混,这里的概念其实是个存储不同玩家的数据的datastore model。


下面是Normcore给的例子

1. [Color] 先用一个model的脚本定义variables,然后需要再inspector里compile一下,normcore会自动生成realtimeModel的代码在这个脚本里。

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using Normal.Realtime;
using Normal.Realtime.Serialization;
 
[RealtimeModel]
public partial class ColorSyncModel
{
    [RealtimeProperty(1, true, true)]
    private Color _color;
}
 
/* ----- Begin Normal Autogenerated Code ----- */
/* Your autogenerated model code will appear here. */
/* ----- End Normal Autogenerated Code ----- */

Tutorial的例子

1. [Scoreboard] + [Player Name]


// Scoreboard Tutorial Case
using Normal.Realtime.Serialization;
using Normal.Realtime;

[RealtimeModel]
public partial class PlayerDataModel_Ju
{
    [RealtimeProperty(1, true, true)]
    private int _playerNumber; // _playerScore
}

[RealtimeModel]
public partial class ScoreboardModel
{
    [RealtimeProperty(1, true, true)]
    private string _scoreBoardText;
}
    
// Player Name Tutorial Case
[RealtimeModel]
public partial class PlayerModel
{
    [RealtimeProperty(1, true, true)]
    private string _playerName;
}
    

2 [Scoreboard] + [Player Name]


// old version
    public class ScoreboardScript : RealtimeComponent
{
    private ScoreboardModel _model;
    public RealtimeAvatarManager _avatarManager;
 private Text _scoreBoardText;

    private void Awake()
    {
    
        // Get a reference to the mesh renderer
        
    }
    
    private ScoreboardModel model
    {
        set
        {
            if(_model != null)
            {
                _model.scoreBoardTextDidChange -= ScoreBoardDidChange;
            }
 
            _model = value;
 
            if(_model != null)
            {
                UpdateScoreBoardText();
                _model.scoreBoardTextDidChange += ScoreBoardDidChange;
            }
        }
    }
 
    private void ScoreBoardDidChange(ScoreboardModel model, string value)
    {
        UpdateScoreBoardText();
    }
 
    private void UpdateScoreBoardText()
    {
        _scoreBoardText.text = _model.scoreBoardText;
    }
    
    private void SetScoreBoardText()
    {
        int playerID = 0;
        _model.scoreBoardText = "";
 
        foreach (var item in _avatarManager.avatars)
        {
            playerID = item.Key + 1;
            _model.scoreBoardText += "Player " + playerID + ": " + _avatarManager.avatars[item.Key].gameObject.GetComponentInChildren<PlayerDataScript>().GetScore() + "\n";        
        }
    }
    
    
}


2. [Color] 随后再用另一个脚本来执行想要执行的命令

// new version
    
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using Normal.Realtime;

public class ColorSync : RealtimeComponent<ColorSyncModel>
    {
        private MeshRenderer _meshRenderer;
 
        private void Awake()
        {
            // Get a reference to the mesh renderer
            _meshRenderer = GetComponent<MeshRenderer>();
        }
 
        protected override void OnRealtimeModelReplaced(ColorSyncModel previousModel, ColorSyncModel currentModel)
        {
            if (previousModel != null)
            {
                // Unregister from events
                previousModel.colorDidChange -= ColorDidChange;
            }
 
            if (currentModel != null)
            {
                // If this is a model that has no data set on it, populate it with the current mesh renderer color.
                if (currentModel.isFreshModel)
                    currentModel.color = _meshRenderer.material.color;
 
                // Update the mesh render to match the new model
                UpdateMeshRendererColor();
 
                // Register for events so we'll know if the color changes later
                currentModel.colorDidChange += ColorDidChange;
            }
        }
 
        private void ColorDidChange(ColorSyncModel model, Color value)
        {
            // Update the mesh renderer
            UpdateMeshRendererColor();
        }
 
        private void UpdateMeshRendererColor()
        {
            // Get the color from the model and set it on the mesh renderer.
            _meshRenderer.material.color = model.color;
        }
 
        public void SetColor(Color color)
        {
            // Set the color on the model
            // This will fire the colorChanged event on the model, which will update the renderer for both the local player and all remote players.
            model.color = color;
        }
    }

OVR Lipsync from discord



7 次查看0 則留言

相關文章

查看全部

Unity_HeadboxVive_0308 //========= Copyright 2019, HTC Corporation. All rights reserved. ===========using System.Collections.Generic; using UnityEngine; namespace ViveSR { namespace anipal { nam

private void RenderModelLipShape(LipShapeTable_v2 lipShapeTable, Dictionary<LipShape_v2, float> weighting) { for (int i = 0; i < lipShapeTable.lipShapes.Length; i++) { int targetIndex