Actually asked for this once but seems like no one gave me the expected answer :( I'm trying to create a OPC UA Client in Unity3D. To be more specific, it will be something like: a simple scene with only a Text. That Text shows the value of a variable read from OPC UA Server. I added this code (found on stackoverflow) but it didn't work.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using System;
using Opc.Ua;
using Opc.Ua.Client;
using Opc.Ua.Configuration;
public class main : MonoBehaviour
{
private async void Start()
{
Console.WriteLine("Step 1 - Create a config.");
var config = new ApplicationConfiguration()
{
ApplicationName = "test-opc",
ApplicationType = ApplicationType.Client,
SecurityConfiguration = new SecurityConfiguration { ApplicationCertificate = new CertificateIdentifier() },
TransportConfigurations = new TransportConfigurationCollection(),
TransportQuotas = new TransportQuotas { OperationTimeout = 15000 },
ClientConfiguration = new ClientConfiguration { DefaultSessionTimeout = 60000 }
};
await config.Validate(ApplicationType.Client);
if (config.SecurityConfiguration.AutoAcceptUntrustedCertificates)
{
config.CertificateValidator.CertificateValidation += (s, e) => { e.Accept = (e.Error.StatusCode == StatusCodes.BadCertificateUntrusted); };
}
Console.WriteLine("Step 2 - Create a session with your server.");
using (var session = await Session.Create(config, new ConfiguredEndpoint(null, new EndpointDescription("opc.tcp://localhost:4841")), true, "", 60000, null, null))
{
Console.WriteLine("Step 3 - Browse the server namespace.");
ReferenceDescriptionCollection refs;
byte[] cp;
session.Browse(null, null, ObjectIds.ObjectsFolder, 0u, BrowseDirection.Forward, ReferenceTypeIds.HierarchicalReferences, true, (uint)NodeClass.Variable | (uint)NodeClass.Object | (uint)NodeClass.Method, out cp, out refs);
Console.WriteLine("DisplayName: BrowseName, NodeClass");
foreach (var rd in refs)
{
Console.WriteLine(rd.DisplayName + ": " + rd.BrowseName + ", " + rd.NodeClass);
ReferenceDescriptionCollection nextRefs;
byte[] nextCp;
session.Browse(null, null, ExpandedNodeId.ToNodeId(rd.NodeId, session.NamespaceUris), 0u, BrowseDirection.Forward, ReferenceTypeIds.HierarchicalReferences, true, (uint)NodeClass.Variable | (uint)NodeClass.Object | (uint)NodeClass.Method, out nextCp, out nextRefs);
foreach (var nextRd in nextRefs)
{
Console.WriteLine("+ " + nextRd.DisplayName + ": " + nextRd.BrowseName + ", " + nextRd.NodeClass);
}
}
Console.WriteLine("Step 4 - Create a subscription. Set a faster publishing interval if you wish.");
var subscription = new Subscription(session.DefaultSubscription) { PublishingInterval = 1000 };
Console.WriteLine("Step 5 - Add a list of items you wish to monitor to the subscription.");
var list = new List<MonitoredItem> {
new MonitoredItem(subscription.DefaultItem) { DisplayName = "aaatime", StartNodeId = "i=10004" } };
list.ForEach(i => i.Notification += OnNotification);
subscription.AddItems(list);
Console.WriteLine("Step 6 - Add the subscription to the session.");
session.AddSubscription(subscription);
subscription.Create();
Console.WriteLine("Finished client initialization");
}
}
private static void OnNotification(MonitoredItem item, MonitoredItemNotificationEventArgs e)
{
foreach (var value in item.DequeueValues())
{
Console.WriteLine("{0}: {1}, {2}, {3}", item.DisplayName, value.Value, value.SourceTimestamp, value.StatusCode);
}
}
}
For my expectation, it will be something like: when I press Play, the Unity app run and connect to my OPC UA Server, then, it reads the value of a variable from Server, and displays that value in Text. So what I do is create a Unity project -> a scene -> a canvas -> a text, a C# Script (code above). But when I add the Script to the Canvas, it is a notification like: can't add script, make sure there are no compiler error and file name and class name are matched. I think I have already checked all of them, but there is not any errors.
Can you suggest me some solution with my problems?
P/S: the code was from this thread: Create a very simple OPC client in Unity3d with opc ua .net library