How do I completelly call the function in a script in Unity from an HTML-page and use the output from that call to generate changes inside the webplayer?
I don’t know if it is possible to do it that way around. Would probably raise some security concerns. However you could do it the other way around and have a function on the web page return updates when called using:
file:///Applications/Unity/Documentation/ScriptReference/Application.ExternalCall.html
The sugested thing isn’t a desired solution.
How do I make a reference to a file on the server that wasn’t on the server at buildtime and then create a view depending on that file?
Ah so you’re really interested in grabbing server data? Use the WWW class to request an url from the server and grab the result from the data property.
But why do I need to use the WWW when the file is put just next to the *.unity3d-file?
If the scripts in Unity are able to locate files in subfolders to the present location why aren’t those scripts able to do the same during runtime just with new files on excactly the same location just on a server this time?
The process for calling a function inside an embedded Unity Webplayer is described here: http://unity3d.com/support/documentation/Manual/Unity%20Web%20Player%20and%20browser%20communication.html
Basically you use document.GetElementById() from your javascript in the web page.
I don’t know what you mean by “create a view”. To make a reference to a file that wasn’t on the server you will need to use some web page scripting. Didn’t we already talk about this in this thread? You’ll need to roll your own solution to upload user files to a server directory, store the url in some kind of SQL database, query that url based on the user’s profile (or whatever tracking you want to use but user accounts are the most common), and ask the runtime to download the file located in the referenced directory. Once it’s downloaded you can instantiate it and do whatever else you want with it inside Unity.
Yes, I already read the
http://unity3d.com/support/documentation/Manual/Unity%20Web%20Player%20and%20browser%20communication.html
but it doesn’t explain all the details good enough for me. Excactly WHAT goes WHERE and WHY?
I tried making the
document.GetElementById()
in the head-section of the html-page but it didn’t seem to work. Probably because I am confused about where to put the functionnames, arguments and everything in the javascript.
We might have talked about the scripting but I’d really like some real coding examples. I don’t use any kind of userprofile or anything. I just upload a file (which I’ve done by now) and then I’d like the Unity-application to reference that file.
The unity plugin is running client-side. It is downloaded by the browser and played from a temporary location. The game therefore has no direct access to the filesystem of the server.
here’s my two cents…
if the page is php, you could get it to echo anything dynamically in response to clicks and other such input.
unity can open read from any page very easily. Right now I’m writing an error handler that listens for different input from a php echo statement.
My project is unity driven, but as long as unity has focus and is running, I bet you could drive it from a page.
try making a function inside unity that listens to the echo from a php and does different things depending on what the echo says.
you would need:
[an interface page] (preferably with unity embedded in it)
[an echo page] (that only echos a single conditional command or string at a time)
The trick would be to use the php interface without unity losing focus.
Maybe that would work.
Probably not the most efficient way to do this though…
Easy with the caps, folks are here trying to help.
The cited documentation page actually does a good job of giving example code and stating where it all goes.
Item #1
Where? In the page that contains the Unity content.
What? Write a JavaScript function that uses getElementById to reference your Unity content. The you use SendMessage off of that reference to “send a message” to (aka: call a function on) a GameObject inside your Unity content. When doing this you will need to provide a GameObject name, a function name and if desired, a data parameter.
Why? You want to use JavaScript to call a function in your Unity content don’t ya? ![]()
Example? See the first script example on the cited doc page.
Item #2
Where? Inside your Unity content, attached to a GameObject with the same name as used in item #1
What? A simple component script that defines a function that has the same name as used in item #1. That function should be prepared to receive and respond to a data argument if necessary.
Why? You said you want to call a function inside your Unity content, here it is. ![]()
Example? See the second script example on the cited doc page.
If you’re having trouble with this then tell us which part(s) you’re stuck on. How to write and call a JavaScript function inside your web page? Or how to have that function call your Unity content? Or both? So far it’s not clear (to me) where you’re having so much trouble.
See Angry Ant’s response. When running in the browser the content is playing locally, the file you want to access is on the server. So if this is truly what you want to do (“read a file off the server and use that data inside Unity”) then you’ll need to use the WWW class and not the mumbo jumbo I offered above. Example:
function GetMyData () {
var tURL = "http://www.yourserver.com/yourfile.txt";
var tWWW = WWW(tURL);
yield (tWWW);
if (tWWW.error == null) {
// use your data!
} else {
// there was an error retrieving your data
}
}
From your posts so far I’m also not sure what exactly you’re wanting to do. At first it seemed you wanted to call a function inside your Unity content from the browser, that’s what I discussed in my reply above. Then later it seems that you want to read in an external file then use that data in your content. That question is answered (I hope) in this post and by Angry Ant.
Sorry if I come off as bad content. That wasn’t my intent. The reason why I wrote in capital letters was simply to underline that excactly those words were the ones that I needed answers for.
About the WWW I suddenly realized last night that Unity Webplayer runs at the cllient and that uploaded files are on the server. That’s why the WWW has to be used.
To explain what I would lilke Unity to offer then a Unity Webplayer that shows both 3d-objects and 2d-images inside the webplayer as a result from both 3d-files and 2d-images the client offers to the webplayer from the clients computer. The solution as I see it so far is thata the clilent uploads the files to the server and then the webplayer downloads the files to the webplayer again, does some transformation of the files if needed and then shows the graphics in the webplayer.
No worries, I tried to keep it light with the smilie and all… It’s all good. ![]()
Bingo!
Please note that you can’t load external model assets on the fly at run-time, they need to be included in your content when you publish it. Being able to do that (pull in models at run-time) is something we’re of course considering for future inclusion in Unity, but it’s not there now. You can definitely load images though, then you can use those images to create textures inside of your content. Take my code example above, use an image URL instead of a *.txt file URL, then when you “use your data” simply use LoadImageIntoTexture to make a texture out of it, then use it how you like!
He can if he acquired my .obj importer script, which he did. ![]()
–Eric
Touché! ![]()
Lets forget about the uploaded file at this moment. How does the client pass arguments into the Unityfile locally in the webplayer? Is the only way around it to do it as described in:
Wed Apr 09, 2008 4:52 am ?
This description looks like the sendMessage() is inside the Unityscript on buildtime. What I would like is to send arguments to the UnityEngine during runtime - that is to do it through c# hopefully or else through the javasccript in the html-page - and then get the results from the UnityEnginegenerating content deeper down in the UnityEngine that will produce objects behave in certain ways in the WebPlayer. Isn’t it possible to pass dynamic arguments to the compiled Unityfile at runtime?
Yes, that post (of mine) explains exactly how you can have browser-based JavaScript call a function within your Unity web player content and pass it some data.
No it’s not, and certainly not based upon my description (I hope!).
Allow me to quote my prior post with enlarged text to clarify things and address this issue:
Note: the large text isn’t meant to shout so much as provide emphasis on the point in question.
Then the cited doc page’s first example script is:
<script type="text/javascript" language="javascript">
<!--
function SaySomethingToUnity()
{
document.getElementById("UnityContent").SendMessage("MyObject", "MyFunction", "Hello from a web page!");
}
-->
</script>
And that is clearly not JavaScript code for use inside of Unity (we don’t use tags
).
So in summary, you reference the Unity content using getElementById then call SendMessage off of that reference, all of that being done in the browser using browser-based JavaScript. Do note that there is a SendMessage function that you can use inside your Unity content to pass messages between GameObjects, but I took great care to explain that in your case you would use SendMessage from within the browser.
Yes it is, please re-read my post and the documentation again if necessary. Then, wrap your head around each item (as I listed them) individually. Don’t worry about item #2 until you understand item #1. Item #1 is scripted in the browser (it sends a message from the browser to your content), item #2 is scripted inside your Unity content (it receives the message sent from the browser).
While wrapping your head around item #1, create a simple test page and feel free to post up the browser-based JavaScript you write (you should be able to just copy-paste-edit the script example above) and we’ll make sure it’s correct before going on to item #2.
Alright. Now I made a html-page with this script in the head-section:
And this code somewhere in the body-section: I'm using the UploadButton-control in the .NET namespace which provides access to a local file. I'd like to send the data of that file into a script in Unity which is called "Test" and set a variable in that script to the path of the submitted file. Then I'd like Unity to rerender the entire content so that the new input in the form of a graphical file is rendered, too. Where do I need to change things in the script and the call to the script to make things work?Ah, I just realized 1 error. The line has to be corrected to:
document.getElementById(“UnityEmbed”).SendMessage(“Test2”, “Obj File URL”, “http://localhost:8080/1x3001-lod0.obj”);
But this doesn’t solve the entire problem…
Instead of “Obj File URL”, that needs to be the name of the actual function.
–Eric