【m】插件上传
This commit is contained in:
102
Packages/com.unity.renderstreaming@3.1.0-exp.9/Samples~/Example/Receiver/AudioSpectrumView.cs
vendored
Normal file
102
Packages/com.unity.renderstreaming@3.1.0-exp.9/Samples~/Example/Receiver/AudioSpectrumView.cs
vendored
Normal file
@@ -0,0 +1,102 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using Unity.Collections;
|
||||
using UnityEngine;
|
||||
using Object = UnityEngine.Object;
|
||||
|
||||
namespace Unity.RenderStreaming.Samples
|
||||
{
|
||||
class AudioSpectrumView : MonoBehaviour
|
||||
{
|
||||
[SerializeField] AudioSource target;
|
||||
[SerializeField] LineRenderer line;
|
||||
[SerializeField] Color[] lineColors;
|
||||
[SerializeField] RectTransform rectTransform;
|
||||
[SerializeField] float xRatio = 1f;
|
||||
[SerializeField] float yRatio = 1f;
|
||||
|
||||
const int positionCount = 256;
|
||||
float[] spectrum = new float[2048];
|
||||
private AudioClip clip;
|
||||
|
||||
Vector3[] array;
|
||||
List<LineRenderer> lines = new List<LineRenderer>();
|
||||
|
||||
private Dictionary<AudioSpeakerMode, int> SpeakerModeToChannel = new Dictionary<AudioSpeakerMode, int>()
|
||||
{
|
||||
{AudioSpeakerMode.Mono, 1},
|
||||
{AudioSpeakerMode.Stereo, 2},
|
||||
{AudioSpeakerMode.Quad, 4},
|
||||
{AudioSpeakerMode.Surround, 5},
|
||||
{AudioSpeakerMode.Mode5point1, 6},
|
||||
{AudioSpeakerMode.Mode7point1, 8},
|
||||
};
|
||||
|
||||
void Start()
|
||||
{
|
||||
array = new Vector3[positionCount];
|
||||
|
||||
// This line object is used as a template.
|
||||
if (line.gameObject.activeInHierarchy)
|
||||
line.gameObject.SetActive(false);
|
||||
|
||||
AudioSettings.OnAudioConfigurationChanged += OnAudioConfigurationChanged;
|
||||
}
|
||||
|
||||
void OnAudioConfigurationChanged(bool deviceChanged)
|
||||
{
|
||||
// reset lines;
|
||||
clip = null;
|
||||
}
|
||||
|
||||
void ResetLines(int channelCount)
|
||||
{
|
||||
foreach (var line in lines)
|
||||
{
|
||||
Object.Destroy(line.gameObject);
|
||||
}
|
||||
lines.Clear();
|
||||
for (int i = 0; i < channelCount; i++)
|
||||
{
|
||||
var line_ = GameObject.Instantiate(line, line.transform.parent);
|
||||
line_.gameObject.SetActive(true);
|
||||
line_.positionCount = positionCount;
|
||||
line_.startColor = lineColors[i];
|
||||
line_.endColor = lineColors[i];
|
||||
lines.Add(line_);
|
||||
}
|
||||
}
|
||||
|
||||
void Update()
|
||||
{
|
||||
if (target.clip == null)
|
||||
{
|
||||
if (lines.Count > 0)
|
||||
ResetLines(0);
|
||||
clip = null;
|
||||
return;
|
||||
}
|
||||
|
||||
if (clip != target.clip)
|
||||
{
|
||||
clip = target.clip;
|
||||
int channelCount = clip.channels;
|
||||
var conf = AudioSettings.GetConfiguration();
|
||||
int maxChannelCount = SpeakerModeToChannel[conf.speakerMode];
|
||||
channelCount = Math.Min(channelCount, maxChannelCount);
|
||||
ResetLines(channelCount);
|
||||
}
|
||||
for (int lineIndex = 0; lineIndex < lines.Count; lineIndex++)
|
||||
{
|
||||
target.GetSpectrumData(spectrum, lineIndex, FFTWindow.Rectangular);
|
||||
for (int i = 1; i < array.Length; i++)
|
||||
{
|
||||
float x = rectTransform.rect.width * i / array.Length * xRatio;
|
||||
float y = rectTransform.rect.height * Mathf.Log(spectrum[i] + 1) * yRatio;
|
||||
array[i] = new Vector3(x, y, 0);
|
||||
}
|
||||
lines[lineIndex].SetPositions(array);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,11 @@
|
||||
fileFormatVersion: 2
|
||||
guid: fca600486dff01449843d95ce65d4a89
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
2174
Packages/com.unity.renderstreaming@3.1.0-exp.9/Samples~/Example/Receiver/Receiver.unity
vendored
Normal file
2174
Packages/com.unity.renderstreaming@3.1.0-exp.9/Samples~/Example/Receiver/Receiver.unity
vendored
Normal file
File diff suppressed because it is too large
Load Diff
7
Packages/com.unity.renderstreaming@3.1.0-exp.9/Samples~/Example/Receiver/Receiver.unity.meta
vendored
Normal file
7
Packages/com.unity.renderstreaming@3.1.0-exp.9/Samples~/Example/Receiver/Receiver.unity.meta
vendored
Normal file
@@ -0,0 +1,7 @@
|
||||
fileFormatVersion: 2
|
||||
guid: d6a3fc68a1f7c05429fd88cc859bfaa1
|
||||
DefaultImporter:
|
||||
externalObjects: {}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
144
Packages/com.unity.renderstreaming@3.1.0-exp.9/Samples~/Example/Receiver/ReceiverSample.cs
vendored
Normal file
144
Packages/com.unity.renderstreaming@3.1.0-exp.9/Samples~/Example/Receiver/ReceiverSample.cs
vendored
Normal file
@@ -0,0 +1,144 @@
|
||||
using System;
|
||||
using UnityEngine;
|
||||
using UnityEngine.UI;
|
||||
|
||||
namespace Unity.RenderStreaming.Samples
|
||||
{
|
||||
static class InputSenderExtension
|
||||
{
|
||||
public static (Rect, Vector2Int) GetRegionAndSize(this RawImage image)
|
||||
{
|
||||
// correct pointer position
|
||||
Vector3[] corners = new Vector3[4];
|
||||
image.rectTransform.GetWorldCorners(corners);
|
||||
Camera camera = image.canvas.worldCamera;
|
||||
var corner0 = RectTransformUtility.WorldToScreenPoint(camera, corners[0]);
|
||||
var corner2 = RectTransformUtility.WorldToScreenPoint(camera, corners[2]);
|
||||
var region = new Rect(
|
||||
corner0.x,
|
||||
corner0.y,
|
||||
corner2.x - corner0.x,
|
||||
corner2.y - corner0.y
|
||||
);
|
||||
|
||||
var size = new Vector2Int(image.texture.width, image.texture.height);
|
||||
return (region, size);
|
||||
}
|
||||
}
|
||||
|
||||
class ReceiverSample : MonoBehaviour
|
||||
{
|
||||
#pragma warning disable 0649
|
||||
[SerializeField] private SignalingManager renderStreaming;
|
||||
[SerializeField] private Button startButton;
|
||||
[SerializeField] private Button stopButton;
|
||||
[SerializeField] private InputField connectionIdInput;
|
||||
[SerializeField] private RawImage remoteVideoImage;
|
||||
[SerializeField] private AudioSource remoteAudioSource;
|
||||
[SerializeField] private VideoStreamReceiver receiveVideoViewer;
|
||||
[SerializeField] private AudioStreamReceiver receiveAudioViewer;
|
||||
[SerializeField] private SingleConnection connection;
|
||||
[SerializeField] private Text resolution;
|
||||
#pragma warning restore 0649
|
||||
|
||||
private string connectionId;
|
||||
private InputSender inputSender;
|
||||
private RenderStreamingSettings settings;
|
||||
private Vector2 lastSize;
|
||||
|
||||
void Awake()
|
||||
{
|
||||
startButton.onClick.AddListener(OnStart);
|
||||
stopButton.onClick.AddListener(OnStop);
|
||||
if (connectionIdInput != null)
|
||||
connectionIdInput.onValueChanged.AddListener(input => connectionId = input);
|
||||
|
||||
receiveVideoViewer.OnUpdateReceiveTexture += OnUpdateReceiveTexture;
|
||||
receiveAudioViewer.OnUpdateReceiveAudioSource += source =>
|
||||
{
|
||||
source.loop = true;
|
||||
source.Play();
|
||||
};
|
||||
|
||||
inputSender = GetComponent<InputSender>();
|
||||
inputSender.OnStartedChannel += OnStartedChannel;
|
||||
|
||||
settings = SampleManager.Instance.Settings;
|
||||
}
|
||||
|
||||
void Start()
|
||||
{
|
||||
if (renderStreaming.runOnAwake)
|
||||
return;
|
||||
|
||||
if (settings != null)
|
||||
renderStreaming.useDefaultSettings = settings.UseDefaultSettings;
|
||||
if (settings?.SignalingSettings != null)
|
||||
renderStreaming.SetSignalingSettings(settings.SignalingSettings);
|
||||
renderStreaming.Run();
|
||||
}
|
||||
|
||||
private void Update()
|
||||
{
|
||||
// Call SetInputChange if window size is changed.
|
||||
var size = remoteVideoImage.rectTransform.sizeDelta;
|
||||
if (lastSize == size)
|
||||
return;
|
||||
lastSize = size;
|
||||
CalculateInputRegion();
|
||||
}
|
||||
|
||||
void OnUpdateReceiveTexture(Texture texture)
|
||||
{
|
||||
remoteVideoImage.texture = texture;
|
||||
CalculateInputRegion();
|
||||
}
|
||||
|
||||
void OnStartedChannel(string connectionId)
|
||||
{
|
||||
CalculateInputRegion();
|
||||
}
|
||||
|
||||
private void OnRectTransformDimensionsChange()
|
||||
{
|
||||
CalculateInputRegion();
|
||||
}
|
||||
|
||||
void CalculateInputRegion()
|
||||
{
|
||||
if (inputSender == null || !inputSender.IsConnected || remoteVideoImage.texture == null)
|
||||
return;
|
||||
var (region, size) = remoteVideoImage.GetRegionAndSize();
|
||||
resolution.text = $"{(int)region.width} x {(int)region.height}";
|
||||
inputSender.CalculateInputResion(region, size);
|
||||
inputSender.EnableInputPositionCorrection(true);
|
||||
}
|
||||
|
||||
private void OnStart()
|
||||
{
|
||||
if (string.IsNullOrEmpty(connectionId))
|
||||
{
|
||||
connectionId = System.Guid.NewGuid().ToString("N");
|
||||
connectionIdInput.text = connectionId;
|
||||
}
|
||||
connectionIdInput.interactable = false;
|
||||
if (settings != null)
|
||||
receiveVideoViewer.SetCodec(settings.ReceiverVideoCodec);
|
||||
receiveAudioViewer.targetAudioSource = remoteAudioSource;
|
||||
|
||||
connection.CreateConnection(connectionId);
|
||||
startButton.gameObject.SetActive(false);
|
||||
stopButton.gameObject.SetActive(true);
|
||||
}
|
||||
|
||||
private void OnStop()
|
||||
{
|
||||
connection.DeleteConnection(connectionId);
|
||||
connectionId = String.Empty;
|
||||
connectionIdInput.text = String.Empty;
|
||||
connectionIdInput.interactable = true;
|
||||
startButton.gameObject.SetActive(true);
|
||||
stopButton.gameObject.SetActive(false);
|
||||
}
|
||||
}
|
||||
}
|
||||
11
Packages/com.unity.renderstreaming@3.1.0-exp.9/Samples~/Example/Receiver/ReceiverSample.cs.meta
vendored
Normal file
11
Packages/com.unity.renderstreaming@3.1.0-exp.9/Samples~/Example/Receiver/ReceiverSample.cs.meta
vendored
Normal file
@@ -0,0 +1,11 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 97627988cbc0b4b15aefdbed5cb090d0
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
Reference in New Issue
Block a user