{"id":1240,"date":"2022-12-02T17:21:57","date_gmt":"2022-12-02T15:21:57","guid":{"rendered":"https:\/\/www.rocworks.at\/wordpress\/?p=1240"},"modified":"2022-12-02T19:49:45","modified_gmt":"2022-12-02T17:49:45","slug":"graphql-for-unity-with-wincc-unified","status":"publish","type":"post","link":"https:\/\/www.rocworks.at\/wordpress\/?p=1240","title":{"rendered":"GraphQL for Unity with WinCC Unified"},"content":{"rendered":"\n<p class=\"wp-block-paragraph\">In this post I will show how data from <a href=\"https:\/\/www.siemens.com\/global\/en\/products\/automation\/simatic-hmi\/wincc-unified.html\">WinCC Unified V18<\/a> can be brought into Unity with the <a href=\"https:\/\/assetstore.unity.com\/packages\/slug\/199115\">GraphQL for Unity Pro<\/a> Asset. <\/p>\n\n\n\n<p class=\"wp-block-paragraph\">First you need WinCC Unified V18 with the GraphQL Server. The GraphQL server comes out of the box with the new version and should be up and running automatically when you download and start your runtime. You should see a process &#8220;WCCILgraphQLServer.exe&#8221; in your task manager. <\/p>\n\n\n\n<p class=\"wp-block-paragraph\">In Unity you have to create a project and download the <a href=\"https:\/\/assetstore.unity.com\/packages\/slug\/199115\">GraphQL for Unity Pro<\/a> Asset in the Package Manager. <\/p>\n\n\n\n<ol class=\"wp-block-list\">\n<li><strong>Prefab<\/strong>: Drag and drop the WinCC Unified Prefab from the Prefabs folder into your scene.<\/li>\n\n\n\n<li><strong>Connection<\/strong>: In the properties you have to set your GraphQL Host, optionally the port, the path (typically \/graphql), and if you want to have a secured TLS connection (HTTPS, WSS). <\/li>\n\n\n\n<li><strong>Websocket<\/strong>: If you want to subscribe to tag value changes, then you have to open additionally a Websocket<strong> <\/strong>connection. Without the Websocket connection you can still read and write tag values, but a subscription to tag value changes is not possible.<\/li>\n\n\n\n<li><strong>Authorization<\/strong>: Set the username and password to connect to WinCC Unified (at the time of writing, this user must have the role HMI Administrator).<\/li>\n\n\n\n<li><strong>Logon<\/strong>: Check the &#8220;Logon&#8221; checkbox, if you want to start the connection at startup (you can also set this &#8220;on-demand&#8221; during runtime in your code.<\/li>\n<\/ol>\n\n\n\n<figure class=\"wp-block-image size-large\"><a href=\"https:\/\/www.rocworks.at\/wordpress\/wp-content\/uploads\/2022\/12\/image-3.png\"><img loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"597\" src=\"https:\/\/www.rocworks.at\/wordpress\/wp-content\/uploads\/2022\/12\/image-3-1024x597.png\" alt=\"\" class=\"wp-image-1244\" srcset=\"https:\/\/www.rocworks.at\/wordpress\/wp-content\/uploads\/2022\/12\/image-3-1024x597.png 1024w, https:\/\/www.rocworks.at\/wordpress\/wp-content\/uploads\/2022\/12\/image-3-300x175.png 300w, https:\/\/www.rocworks.at\/wordpress\/wp-content\/uploads\/2022\/12\/image-3-768x448.png 768w, https:\/\/www.rocworks.at\/wordpress\/wp-content\/uploads\/2022\/12\/image-3-1536x896.png 1536w, https:\/\/www.rocworks.at\/wordpress\/wp-content\/uploads\/2022\/12\/image-3.png 1835w\" sizes=\"auto, (max-width: 1024px) 100vw, 1024px\" \/><\/a><\/figure>\n\n\n\n<p class=\"wp-block-paragraph\">Then you can already start the project in the editor to see if the connection can be established. If everything works fine, then the &#8220;Logged On&#8221; property turns to checked.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Now you can create your own C# script and read\/write\/subscribe tag values in an easy way in C# scripting. In that case I have simple added the script as additional component to the WinCC Unified GameObject (Prefab). <\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>using System;\nusing System.Collections;\nusing System.Collections.Generic;\nusing UnityEngine;\nusing GraphQLWinccUnified;\nusing Newtonsoft.Json.Linq;\n\npublic class WinccUnified3 : MonoBehaviour\n{\n    private WinccUnified _connection;\n    private bool _ready;\n\n    \/\/ Start is called before the first frame update\n    void Start()\n    {\n        _connection = GetComponent&lt;WinccUnified&gt;();\n    }\n\n    \/\/ Update is called once per frame\n    async void Update()\n    {\n        if (!_ready &amp;&amp; _connection.IsWebsocketReady()) \n        {\n            _ready = true;\n\n            \/\/ SubscribeTagValues\n            _connection.SubscribeTagValues(\n                new string&#91;] { \"HMI_String_1\" }, \n                data =&gt; { Debug.Log(data.GetValue&lt;string&gt;()); });\n\n            \/\/ WriteTagValues\n            await _connection.WriteTagValues(\n                new string&#91;] { \"HMI_String_1\" },\n                new JValue&#91;] { new JValue(\"Hello World!\") },\n                new DateTime&#91;] { DateTime.Now });\n\n            \/\/ ReadTagValues\n            var result = await _connection.ReadTagValues(\n                new string&#91;] { \"HMI_String_1\" });\n            Debug.Log(result&#91;0].GetValue&lt;string&gt;());\n        }\n    }\n}<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">If you deal with with a <strong>self-signed<\/strong> certificate, then you must uncheck &#8220;Validate Certificate&#8221; at the &#8220;Graph QL Http&#8221; Component. But the Websocket connection in Unity does not yet support self-signed certificates. So, it is better to use an insecure connection. Typically the GraphQL port is 4000, so be sure to set the Port to <strong>4000<\/strong>. And don&#8217;t forget to open the firewall for that port on the host where the Unified runtime is running.<\/p>\n\n\n\n<figure class=\"wp-block-image size-full\"><a href=\"https:\/\/www.rocworks.at\/wordpress\/wp-content\/uploads\/2022\/12\/image-4.png\"><img loading=\"lazy\" decoding=\"async\" width=\"812\" height=\"674\" src=\"https:\/\/www.rocworks.at\/wordpress\/wp-content\/uploads\/2022\/12\/image-4.png\" alt=\"\" class=\"wp-image-1249\" srcset=\"https:\/\/www.rocworks.at\/wordpress\/wp-content\/uploads\/2022\/12\/image-4.png 812w, https:\/\/www.rocworks.at\/wordpress\/wp-content\/uploads\/2022\/12\/image-4-300x249.png 300w, https:\/\/www.rocworks.at\/wordpress\/wp-content\/uploads\/2022\/12\/image-4-768x637.png 768w\" sizes=\"auto, (max-width: 812px) 100vw, 812px\" \/><\/a><\/figure>\n","protected":false},"excerpt":{"rendered":"<p>In this post I will show how data from WinCC Unified V18 can be brought into Unity with the GraphQL for Unity Pro Asset. First you need WinCC Unified V18 with the GraphQL Server. The GraphQL server comes out of &hellip; <a href=\"https:\/\/www.rocworks.at\/wordpress\/?p=1240\">Continue reading <span class=\"meta-nav\">&rarr;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[52,33],"tags":[],"class_list":["post-1240","post","type-post","status-publish","format-standard","hentry","category-graphql","category-wincc-unified"],"_links":{"self":[{"href":"https:\/\/www.rocworks.at\/wordpress\/index.php?rest_route=\/wp\/v2\/posts\/1240","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.rocworks.at\/wordpress\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.rocworks.at\/wordpress\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.rocworks.at\/wordpress\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.rocworks.at\/wordpress\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=1240"}],"version-history":[{"count":6,"href":"https:\/\/www.rocworks.at\/wordpress\/index.php?rest_route=\/wp\/v2\/posts\/1240\/revisions"}],"predecessor-version":[{"id":1252,"href":"https:\/\/www.rocworks.at\/wordpress\/index.php?rest_route=\/wp\/v2\/posts\/1240\/revisions\/1252"}],"wp:attachment":[{"href":"https:\/\/www.rocworks.at\/wordpress\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=1240"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.rocworks.at\/wordpress\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=1240"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.rocworks.at\/wordpress\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=1240"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}